简体   繁体   English

jQuery选择器帮助,用于选择框

[英]Help with jQuery selector for select box

Using the .change() event I want to store the value of an attribute of the currently selected option. 使用.change()事件,我想存储当前选定选项的属性值。 Please help me finish the below code. 请帮助我完成以下代码。

$("#someid").change(function() {
    var myNumber;
});

If you want need it outside the handler, you'll need to declare the variable outside. 如果要在处理程序外部使用它,则需要在外部声明变量。

var myNumber;

$("#someid").change(function() {
    myNumber = this.options[ this.selectedIndex ].getAttribute('myAttr');
    // or
    myNumber = $(this).find('option:selected').attr('myAttr');
});

If you only need it inside, then declare the variable inside. 如果仅在内部需要它,则在内部声明变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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