使用jQuery隐藏元素;不起作用


Hiding an element with jQuery doesn't work

我正试图创建一个脚本来开发链式选择,但最简单的方法行不通。请注意,a对js和jquery知之甚少。

我用CodeIgniter:<?php echo form_dropdown('city', array(), "", 'id="ciudades"'); ?> 创建下拉列表

然后加载脚本:

if (isset($add_select_sources))
{
    echo "
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'></script>
    <script src='" . $root_path . "js/jquery-1.10.2.js'></script>
    <script type='text/javascript' src='" . $root_path . "js/select.js'></script>
    ";
}

我确信add_select_sources是真的,它经过了测试。

然后我的select.js:$('#ciudades').hide();

我做错了什么?

确保在包含jQuery之后执行javascript。

此外,您应该在"文档就绪关闭"中执行jQuery:

$(document).ready(function(){
    $('#ciudades').hide();
});

试图这样做的人一直存在问题。因此,我还想提出一些建议:

  • 确保你的元素有一个宽度/高度/显示块等
  • 尝试$('#ciudades').show().hide();因为有些人也有这个问题
  • console.log()您的事件,以查看它们是否被激发:
    $(document).ready(function(){
        console.log('doc ready');
        $('#ciudades').show().hide();
        console.log('element hidden');
    });

另外,为什么要使用PHP来回显脚本标记?

这很容易做到。

$(document).ready(function(){
    $('#yourEelementId').hide();
});