admin管理员组文章数量:1389819
When you click on li, I want input radio to be clicked.
However, I am getting an error from conole log saying:
Uncaught RangeError: Maximum call stack size exceeded
How to fix this?
Here the html code:
<ul class="Method">
<li class="shipping_today active">
<label> Label 1 </label>
<input value="shipping_today" name="shipping" type="radio" />
</li>
<li class="shipping_next_month">
<label> Label 2 </label>
<input value="shipping_next_month" name="shipping" type="radio" />
</li>
</ul>
Jquery:
$(".Method li").click(function() {
var thisLi = $(this);
var radio = $(this).find("input:radio");
if (radio.val() == "shipping_today") {
$(".Method li").eq(1).removeClass("active");
$(this).addClass("active");
}
if (radio.val() == "shipping_next_month") {
$(".Method li").eq(-2).removeClass("active");
$(this).addClass("active");
}
radio.click(); //problem here...
});
Is my jQuery code good? what can be improved?
thanks.
When you click on li, I want input radio to be clicked.
However, I am getting an error from conole log saying:
Uncaught RangeError: Maximum call stack size exceeded
How to fix this?
Here the html code:
<ul class="Method">
<li class="shipping_today active">
<label> Label 1 </label>
<input value="shipping_today" name="shipping" type="radio" />
</li>
<li class="shipping_next_month">
<label> Label 2 </label>
<input value="shipping_next_month" name="shipping" type="radio" />
</li>
</ul>
Jquery:
$(".Method li").click(function() {
var thisLi = $(this);
var radio = $(this).find("input:radio");
if (radio.val() == "shipping_today") {
$(".Method li").eq(1).removeClass("active");
$(this).addClass("active");
}
if (radio.val() == "shipping_next_month") {
$(".Method li").eq(-2).removeClass("active");
$(this).addClass("active");
}
radio.click(); //problem here...
});
Is my jQuery code good? what can be improved?
thanks.
Share Improve this question asked Oct 16, 2011 at 12:11 I'll-Be-BackI'll-Be-Back 10.8k42 gold badges118 silver badges227 bronze badges3 Answers
Reset to default 8That's an infinite loop, because the click
event you trigger on the radio button bubbles up to the <li>
element and causes your handler to run recursively.
One solution would be to only relay the click
event if its doesn't e from the radio button itself:
$(".Method li").click(function(event) {
var thisLi = $(this);
var radio = $(this).find("input:radio");
// [...]
if (radio[0] !== event.target) {
radio.click();
}
});
If you only want to check the radio button, however, relaying the event is not necessary. You can use prop() instead:
radio.prop("checked", true);
To prevent event bubbling, you can use stopPropagation():
$(".Method li").click(function(evt) {
evt.stopPropagation();
...
});
Your code needs tweaks, try this:
$(".Method li").click(function() {
var thisLi = $(this);
var radio = $(this).find("input:radio");
if (radio.val() == "shipping_today") {
$(".Method li").eq(1).removeClass("active");
thisLi.addClass("active");
radio.attr("checked", true);
}
else if (radio.val() == "shipping_next_month") {
$(".Method li").eq(0).removeClass("active");
thisLi.addClass("active");
radio.attr("checked", true);
}
});
You can place radio.attr() only once outside of condition if all LI tags contain radio buttons.
本文标签: javascriptWhen clicking on on the LI then automatically click on input radioStack Overflow
版权声明:本文标题:javascript - When clicking on on the LI then automatically click on input radio - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744682304a2619485.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论