开发者

Jquery Grabbing id of a select where there is more than one select with the same class name

开发者 https://www.devze.com 2023-02-20 04:53 出处:网络
I have a form that has elements cloned using jquery. So there could multiple <select> with the same class name but different id eg. <select name=\"ticket1\" id=\"ticket1\" class=\"ticket\">

I have a form that has elements cloned using jquery. So there could multiple <select> with the same class name but different id eg. <select name="ticket1" id="ticket1" class="ticket">

The choice from a select shows/hides a <div id="hide1" class="hide">...</div> This jquery code...

$(document).ready(function(){
  $('.hide').hide();
  $('select.ticket').change(function(){

    var hideNo=this.id.substr(6);
   console.log('This='+this);    //debug for firebug
   console.log('Id='+hideNo);//debug for firebug
    console.log('Value='+this.value);//debug for firebug
开发者_JAVA百科    $('#hide'+hideNo).toggle(this.value == 'child'||this.value == 'youth');
  });
  $(".ticket").change();
});

Works for the first id, but not any subsequent ones.

Whay am I doing wrong please?


Try changing the third line in your code above to:

$('select.ticket').live("change", function(){


Either use live() or delegate() so the event handler works for any elements that match the selector currently or added in the future.

Another option, if you are using the built in .clone() method. You can use .clone(true) to clone any data and events tied to that element.

0

精彩评论

暂无评论...
验证码 换一张
取 消