Jquery 附加选择选项并选择附加选项后,警报显示未定义+;滑动分页
我是Jquery的新手,我正在尝试附加一个select选项。当我在“选择”菜单中选择新添加的选项时,这会起作用。我正处于警戒状态Jquery 附加选择选项并选择附加选项后,警报显示未定义+;滑动分页,jquery,Jquery,我是Jquery的新手,我正在尝试附加一个select选项。当我在“选择”菜单中选择新添加的选项时,这会起作用。我正处于警戒状态 var selects = $(this).closest('div').find('.accSelectandlookup') ; //Check if the accid is already present in the selectoptions and if not then add if (!selects.find('option[value="
var selects = $(this).closest('div').find('.accSelectandlookup') ;
//Check if the accid is already present in the selectoptions and if not then add
if (!selects.find('option[value="' + accid + '"]').length) {
alert(accid); // This throws up the correct value
selects.append('<option value="' + accid + '">Attach to Existing : ' + accname + '</option>');
}
只需使用$(this).val(
)即可获得当前选择的选项
值。由于作用域可变,当触发change
事件时,selects
可能不可用
selects.change(function(){
alert('selected value is ' + $(this).val()); // This throws undefined
});
加载.change()
函数时,它读取当前DOM
当您添加时,它会更改DOM,但不会更新.change()
正在查看的内容。所以你需要像这样修改它
$('body').change(function(){
alert('blah');
}, '.accSelectandlookup');
这将使用当前DOM保持.change()
活动。在控制台中记录选择,并确保正确获取选择元素。
$('body').change(function(){
alert('blah');
}, '.accSelectandlookup');