Jquery 选择更改未触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19194177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery select change not firing
提问by Pete Ravenscroft
I need to capture when a select box changes, should be simple!
我需要在选择框更改时捕获,应该很简单!
$('#multiid').change(function(){
alert('Change Happened');
});
But it does not work, I suspected the problem is that the select box does not exist on document ready it is created only if needed, so I created it empty in HTML, populated it with code as a test but that didn't work either.
但它不起作用,我怀疑问题在于文档准备就绪时不存在选择框,仅在需要时才创建它,所以我在 HTML 中将其创建为空,并用代码填充它作为测试,但也不起作用.
function buildmulti(id,name,price) {
// build action items for action bar
var optlist = $('<select></select>').attr('id', 'multiid').attr('name', 'multiid');
optlist.append('<option value="0">Select Size</option>');
$.each(option, function(index, val) {
if(val.prodID *1 == id * 1) {
v = val.ID;
fprice = price * 1 + val.pricechange * 1;
t = name + ' - ' + val.variation + ' - ' + currency + (fprice).toFixed(2);
optlist.append('<option value="' + v + '">' + t + '</option>');
}
})
$('#addbasket').append(optlist);
};
it's probably another bracket out of place, but I can't find it!
它可能是另一个不合适的支架,但我找不到它!
回答by Dhaval Marthak
Try
尝试
$(document).on('change','#multiid',function(){
alert('Change Happened');
});
As your select-box is generated from the code, so you have to use event delegation, where in place of $(document)
you can have closest parent element.
由于您的选择框是从代码生成的,因此您必须使用事件委托,$(document)
您可以在其中使用最接近的父元素。
Or
或者
$(document.body).on('change','#multiid',function(){
alert('Change Happened');
});
Update:
更新:
Second one works fine, there is another change of selector to make it work.
第二个工作正常,还有另一个选择器更改以使其工作。
$('#addbasket').on('change','#multiid',function(){
alert('Change Happened');
});
Ideally we should use $("#addbasket")
as it's the closest parent element [As i have mentioned above].
理想情况下,我们应该使用$("#addbasket")
它,因为它是最近的父元素 [正如我上面提到的]。
回答by Anil kumar
回答by Prince Patel
You can fire chnage event by these methods:
您可以通过以下方法触发 chnage 事件:
First
第一的
$('#selectid').change(function () {
alert('This works');
});
Second
第二
$(document).on('change', '#selectid', function() {
alert('This Works');
});
Third
第三
$(document.body).on('change','#selectid',function(){
alert('This Works');
});
If this methods not working, check your jQuery working or not:
如果此方法不起作用,请检查您的 jQuery 是否工作:
$(document).ready(function($) {
alert('Jquery Working');
});
回答by Rituraj ratan
回答by dsf
This works for me!
这对我有用!
$('#<%= ddlstuff.ClientID %>').change(function () {
alert('Change Happened');
$('#<%= txtBoxToClear.ClientID %>').val('');
});