简体   繁体   中英

when using variable as selectors, this.value becomes undefined

$(document).on("change","#groupid3,#groupid2,#groupid1",function(){

   alert(this.value);

}

When I doing this , the this.value is proper.

However, if I want to dynamically generate a variable to store the id "#groupid3,#groupid2,#groupid1" as follow,

txt="#groupid3,#groupid2,#groupid1";
$(document).on("change",txt,function(){

   alert(this.value);

}

the this.value is undefined.

Can anyone tell me why this is not working?Thanks !

Check again it working.

<div>
  <select name="tuanpham" id="groupid1">
    <option value="1">1</option>
    <option value="1">2</option>
    <option value="1">3</option>
  </select>
  <select name="tuanpham" id="groupid2">
    <option value="1">1</option>
    <option value="1">2</option>
    <option value="1">3</option>
  </select>
  <select name="tuanpham" id="groupid3">
    <option value="1">1</option>
    <option value="1">2</option>
    <option value="1">3</option>
  </select>
</div>
  <script>
  txt="#groupid3,#groupid2,#groupid1";
      $(document).on("change",txt,function(){
        alert(this.value);
      });

  </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM