[英]Stop Propagation on Input Click
I have an html table, and when a user clicks a table row, the row's checkbox is checked, and some other functionality occurs. 我有一个html表,当用户单击表行时,该行的复选框已选中,并且发生了其他一些功能。 When a user clicks on a select or input, I would like to stop the propagation so the checkbox does not change.
当用户单击选择或输入时,我想停止传播,以便复选框不会更改。 I have demonstrated the functionality I am looking for in this JSFiddle:
我已经在此JSFiddle中演示了我正在寻找的功能:
http://jsfiddle.net/ehf7pc3f/6/ http://jsfiddle.net/ehf7pc3f/6/
On my development site, when a user clicks a select, the checkbox is changed (as intended), but if a user clicks an input, the checkbox does not change. 在我的开发站点上,当用户单击一个选择时,该复选框将更改(按预期方式),但是如果用户单击一个输入,则该复选框不会更改。 I have this code to stop the checkbox from changing:
我有以下代码可阻止复选框更改:
$("select").click(function (e) {
e.stopPropagation();
});
$("input").click(function (e) {
e.stopPropagation();
});
Does anyone have an idea as to why the select click works, but the input click does not? 有谁知道为什么选择单击有效,但输入单击无效?
Update* *更新
JSFiddle: http://jsfiddle.net/ehf7pc3f/8/ JSFiddle: http : //jsfiddle.net/ehf7pc3f/8/
I have added a new JSFiddle that fires an alert when the input is clicked and an alert when a row is clicked. 我添加了一个新的JSFiddle,它在单击输入时触发警报,而在单击行时触发警报。 In the fiddle, if I click the input, only the input alert fires.
在小提琴中,如果单击输入,则仅触发输入警报。 If I do this same test on my development site, only the row alert fires.
如果我在开发站点上进行了相同的测试,则仅触发行警报。
Here's the actual code on my development site: 这是我的开发站点上的实际代码:
<table class="full-width row-table" style="table-layout:fixed;">
<tr style="background:none;" class="no-hover row">
<td class="no-padding" style="width:16px;"><input class="check-box" style="margin:2px 0px;" type="checkbox" /></td>
<td class="no-padding" style="width:6px;"><span class="arrow2" style="margin:6px 0px 0px 0px;"></span></td>
<td style="width:94px;">
<span class="contact-label">Owner <br /> </span>
<select class="contact-drop-down hidden">
<option value="Owner">Owner</option>
<option value="Applicant">Applicant</option>
<option value="Contractor">Contractor</option>
</select>
</td>
<td style="width:95px;">
<p class="title-label">Owner</p>
</td>
<td style="width:119px;">
<p class="name-label">Michael Douglas</p>
</td>
</tr>
</table>
Use event.preventDefault()
. 使用
event.preventDefault()
。
The event.stopPropagation()
stops the event from bubbling up the parent elements in the DOM (or down if you're listening during capture phase). event.stopPropagation()
阻止事件使DOM中的父元素冒泡(如果在捕获阶段正在侦听则冒泡)。 While event.preventDefault()
stops default behaviour by the browser. 当
event.preventDefault()
停止浏览器的默认行为时。
Further reading: 进一步阅读:
https://developer.mozilla.org/en-US/docs/Web/API/event.preventDefault https://developer.mozilla.org/en-US/docs/Web/API/event.preventDefault
https://developer.mozilla.org/en-US/docs/Web/API/event.stopPropagation https://developer.mozilla.org/en-US/docs/Web/API/event.stopPropagation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.