jquery
2011-11-24 143 views 1 likes 
1

我試圖將change事件綁定到選擇標記,但選項很少。如果我將它綁定到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 
}); 

那裏有什麼問題?

+0

uhmm我把你的代碼中的jsfiddle和它的作品... – Jorge

回答

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

+0

的jsfiddle很酷的東西,謝謝!你的建議是非常有用的,但我的代碼不起作用。也許模板的問題比這裏描述的更復雜。 – I159

+1

$(function(){})不是$(document).ready(function(){})的別名嗎? – k4t434sis

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> 
相關問題