简体   繁体   English

触发已使用jQuery自动检查的输入的onclick函数

[英]Trigger onclick function of input that has been checked automatically with jQuery

I have a form with some checkboxes which go like this: 我有一个带有一些复选框的表单,如下所示:

<input type="checkbox" id="id6" value="3" onclick="optionselect('day','3','id6','50')" />

Conditioned on another input I can have these checkboxes checked automatically but for some reason I can't trigger the "optionselect". 以另一个输入为条件,我可以自动选中这些复选框,但是由于某些原因,我无法触发“ optionselect”。

I've tried some of the following, but nothing works: 我已经尝试了以下一些方法,但是没有任何效果:

$("#id6").prop("checked", true).trigger("click");
$("#id6").prop("checked", true).triggerHandler("click");
$("#id6").prop("checked", true).click();

FYI, "optionselect" just does a bit of math in some other text inputs. 仅供参考,“ optionselect”在其他一些文本输入中只是做一些数学运算。

I think that problem is that you should use onchange event in this case, not click . 我认为问题是在这种情况下,您应该使用onchange事件,而不是click Try this: 尝试这个:

<input type="checkbox" id="id6" value="3" onchange="optionselect('day','3','id6','50')" />

and

$('#id6').prop('checked', true).change();

Basically .click() effectively unchecks checkbox after you checked it with prop('checked', true) . 基本上.click()使用prop('checked', true)复选框后,实际上会取消选中该复选框。

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

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