如何确定 Jquery 中另一个元素触发的单选框的状态?
所以协议是我通过单击 div 元素来更新单选框元素。 例如,我有 3 个 div,每个 div 内有 1 个单选框。
当您单击 div 时,单选框会被选中,但问题是这种方式 .change() 事件不起作用。
当我知道特定单选框丢失时如何获得结果:检查状态?
这是我目前拥有的代码:
$('# payment_method_cards, # payment_method_ibank_swe, # payment_method_bp').click(function(){ $(this).children('input[name=" payment_method"]').attr('已检查','已检查'); $('input[name=" payment_method"]').change(function(){ if($(this).val() == 'bp'){ 警报(“就是这个”); } } });
仅当我单击单选框内部时才会触发单选框更改,但在 div 上时不会触发。
So the deal is that i update radiobox elements by clicking on div elements.
So for example i have 3 divs and there's 1 radio box inside each div.
When you click on div the radio box gets checked, but problem is that this way .change() event does not work.
How would i get result where i know when particular radiobox has lost :checked status?
Here is code that i currently have:
$('#payment_method_cards, #payment_method_ibank_swe, #payment_method_bp').click(function(){ $(this).children('input[name="payment_method"]').attr('checked','checked'); $('input[name="payment_method"]').change(function(){ if($(this).val() == 'bp'){ alert("Thats the one"); } } });
The radiobox change only triggered if i click inside radiobox but not when on div.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您有一个与
div
绑定的事件处理程序,该事件处理程序会触发click
或添加checked
属性。为什么在更改单选按钮的状态后不手动触发change()
函数?更新:使用您提供的代码,您将执行以下操作(由于OP的评论而再次更新):
您应该定义
更改
click
处理程序之外的处理程序:I'm assuming you have an event handler tied to the
div
that triggersclick
or adds thechecked
attribute. Why not trigger thechange()
function manually after changing the state of the radio button?Update: With the code you provided, you would do something like this (updated again due to comment from OP):
You should define the
change
handler outside theclick
handler:使用标签代替 DIV:
在上面的示例中,单击单词“Male”将触发更改事件。
我建议使用标签,但如果您坚持使用 DIV,此代码将适合您。仅当事先未选中单选框时才会触发更改事件。
Use labels instead of the DIVs:
In the above example, clicking on the word "Male" will trigger the change event.
I recommend using labels, but if you insist on using DIVs, this code will work for you. The change event is triggered only if the radio box was not checked beforehand.