更改事件不适用于选择

时间:2011-11-24 22:16:20

标签: jquery

我尝试将change事件绑定到select标记,但选项很少。如果我将它绑定到select标签,则它不起作用。如果我将click事件绑定到H1标记,它就可以了。

HTML

<div class="subtitle_block">
  <span>Резюме по разделам</span>
    <form name='region_filter' action='/jobseek/search_cv/' method='post'>                                                
      <select id="id_region" class="" name="region">
        <option value="" selected="selected">Все</option>
        <option value="3">Донецк</option>
        <option value="2">Киев</option>
        <option value="1">Харьков</option>
      </select>                    
    </form>
  <div class="clear_both"></div>
</div>

我试过了那个

$(function(){
    console.log($('#id_region')); //this is works
    $('#id_region').bind('change', function(){console.log('asdf')}); //this is not works
});

就像那样

$(function(){
console.log($('#id_region')); //this is works
$('#id_region').change(function(){console.log('asdf')}); //this is not works
});

即便如此

$(function(){
console.log($('#id_region')); //this is works
$('#id_region').click(function(){console.log('asdf')}); //this is not works
});

那里有什么问题?

2 个答案:

答案 0 :(得分:2)

尝试将代码放在$(document).ready()

$(document).ready(function() {
  console.log($('#id_region')); //this is works
  $('#id_region').change(function(){console.log('asdf')}); //this is not works
});

请参阅this working jsfiddle

答案 1 :(得分:0)

这对我有用:

<body>
        <div class="subtitle_block">
          <span>Резюме по разделам</span>
            <form name='region_filter' action='/jobseek/search_cv/' method='post'>                                                
              <select id="id_region" class="" name="region">
                <option value="" selected="selected">Все</option>
                <option value="3">Донецк</option>
                <option value="2">Киев</option>
                <option value="1">Харьков</option>
              </select>                    
            </form>
          <div class="clear_both"></div>
        </div>
        <script type="text/javascript">
        $(function(){
            $('#id_region').bind("change", null, function() {
                alert('event fired');
            });
        });
        </script>
    </body>