我是Jquery的新手,我正在尝试附加一个选择选项。这是有效的,当我在选择中选择新添加的选项时。我在警报中未定义。
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>');
}
我做错了什么?
提前致谢
selects.change(function(){
alert('selected value is ' + selects.filter(":selected").val()); // This throws undefined
})
答案 0 :(得分:1)
只需使用$(this).val(
)即可获得当前option
的所选select
值。由于范围可变,selects
事件被触发时,change
可能无法使用。
selects.change(function(){
alert('selected value is ' + $(this).val()); // This throws undefined
});
答案 1 :(得分:1)
加载.change()
函数时,它会读取当前的DOM。
当您append
时,它正在更改DOM,但这不会更新.change()
正在查看的内容。所以你需要像这样修改它......
$('body').change(function(){
alert('blah');
}, '.accSelectandlookup');
使用当前DOM保持.change()
LIVE。