简体   繁体   English

我应该对哪个事件进行更改?

[英]which event should I fire a change on?

In an html form I have a checkbox that has a label. 在html表单中,我有一个带有标签的复选框。 When you click the checkbox or its label, it toggles the checkedness of the checkbox. 当您单击复选框或其标签时,它将切换复选框的选中状态。 It's wrapped in a <div> . 它包装在<div>

I am using jquery to hide another element of the form if the checkbox is checked. 如果选中了复选框,我正在使用jquery隐藏表单的另一个元素。 Originally I was using the .click() function of the wrapping div. 最初,我使用包装div的.click()函数。 But I noticed that you could click between the checkbox and the label ( which is still within the div), and get the other to hide, even though the checkbox was not checked. 但是我注意到您可以复选框和标签(仍在div内) 之间单击,并隐藏另一个标签,即使未选中该复选框也是如此。 (Also, for some reason, clicking on the label caused the element to hide then reappear, but I don't think I need to open that can of worms). (此外,由于某种原因,单击标签会导致该元素隐藏然后重新出现,但我认为我不需要打开蠕虫的罐头)。

I was thinking that instead of the click() of the div, I should look at both the checkbox and the div separately. 我当时在考虑,而不是div的click() ,我应该分别查看复选框和div。 Then I thought I just might look for something like an onChange event for the checkbox itself, which clicking the label should affect. 然后,我想我可能会为复选框本身寻找类似onChange事件的内容,单击标签应该会影响该事件。 Then I thought that people have probably solved this problem before and I should not try to re-invent the wheel. 然后我以为人们以前可能已经解决了这个问题,所以我不应该尝试重新发明轮子。

What's a simple way to keep the checkbox value and the hide/show state of my element in sync, when two different elements can change the checkbox status? 当两个不同的元素可以更改复选框状态时,使复选框值和元素的隐藏/显示状态保持同步的简单方法是什么?

在此实例中,复选框的onChange事件可能会很好地满足您的需求。

Attach a onclick event to the checkbox (a onchange event works too). onclick事件附加到复选框( onchange事件也适用)。 Whether you click on the checkbox itself, or its label, the event handler will fire. 无论您单击复选框本身或其标签,事件处理程序都会触发。 Just make sure that the label's for attribute contains the id of the checkbox. 只需确保标签的for属性包含复选框的ID即可。

http://jsfiddle.net/FishBasketGordo/GJbce/ http://jsfiddle.net/FishBasketGordo/GJbce/

尝试在复选框输入上使用.change()函数。

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

相关问题 使用addEventListener添加的fire change事件 - Fire change event which was added using addEventListener 如何正确触发更改事件以触发单元测试中的功能? - How should I correctly trigger a change event to fire a function in unit test? 用户停止键入后应立即触发的jQuery文本框事件? - Jquery textbox event which should fire as soon as user stop typing? 如何检查哪个元素导致on change事件触发 - How to check which element caused an on change event to fire 我应该在等待事件发生时明确地创建一个承诺吗? - Should I explicitly create a promise when waiting for an event to fire? 我如何知道哪个元素从js中触发事件? - How can I know which element fire the event from js? 更改还是关注jQuery? 我应该使用哪个事件与select下拉列表来调用jQuery-AJAX函数? - Change or Focus in jQuery? Which event should I use with select dropdown to give a call to jQuery-AJAX function? 我应该使用哪个HTML事件监听器? - Which HTML event listener should I use? 使用angularjs,如何触发文件上传指令的更改事件? - With angularjs, how do I fire a change event for a file upload directive? 选择代码的形式,如果其中任何一个发生更改,我想触发一个事件 - Form of select tags, I want to fire an event if any of them change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM