[英]How can I prevent jquery from selecting a parent when I click on one of the children in that parent?
ok quick scenario: 好快速方案:
html: HTML:
<span class="answer">blah<input type="radio" value="1"></span>
jquery: jQuery的:
$("span.answer").click(
function() {
check = $("input", this);
check.attr('checked', check.attr('checked') === true ? false : true);
);
Ok so this will check/uncheck the child radio input inside the selected span when I click inside it. 好的,这样当我点击它内部时,这将检查/取消选中所选范围内的子无线电输入。
The problem I have is that I don't want to run this event when I actually click on the radio button itself (obviously because jquery will see the radio button as checked and uncheck - in effect the exact opposite of what should happen usually). 我遇到的问题是,当我实际点击单选按钮本身时,我不想运行此事件(显然因为jquery会将单选按钮视为已选中并取消选中 - 实际上与通常应该发生的情况完全相反)。 Something along the lines of this: 有点像这样:
$("span.answer:not(span input)").click
This of course doesn't work. 这当然不起作用。 Any ideas? 有任何想法吗? Thanks in advance! 提前致谢!
Leo 狮子座
$("span.answer input").click(function(evt)
{
evt.stopPropagation();
});
Will prevent the click event from bubbling up (and triggering the handler on "span.answer".) 将阻止click事件冒泡(并在“span.answer”上触发处理程序。)
There is a specific html tag to accomplish what you try to do with jquery and javascript .. 有一个特定的html标记来完成你尝试用jquery和javascript做的事情。
it is the label
tag 它是label
标签
<label>blah<input type="radio" value="1" /></label>
this will have the effect you want 这将产生你想要的效果
[update] For Internet Explorer 6 to play nice use the complete syntax of the label by using the for
attribute which targets the id of an form input/select/etc.. element.. [更新]为了让Internet Explorer 6更好地使用标签的完整语法,使用for
属性,该属性以表单输入/ select / etc ..元素的id为目标。
<label for="radio_1">blah<input id="radio_1" type="radio" value="1" /></label>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.