[英]Call function on previously selected radio button with the same name
Hello I have some radio buttons with different radio groups name. 您好,我有一些具有不同无线电组名称的单选按钮。 I want to call the function on the previously selected radio button for that specific group.
我想在该特定组的先前选择的单选按钮上调用该函数。
If I check the radio button then select the different radio button from the same group. 如果我选中单选按钮,则从同一组中选择其他单选按钮。 I want call the function or add class on the previously selected radio button of the same group in jQuery.
我想在jQuery中同一组的先前选择的单选按钮上调用函数或添加类。
Here is the example code - 这是示例代码-
RadioGroup1Button 1: <input type='radio' name='radioGroup1' /><br />
RadioGroup1Button 2: <input type='radio' name='radioGroup1' /><br />
RadioGroup2Button 1: <input type='radio' name='radioGroup2' /><br />
RadioGroup2Button 2: <input type='radio' name='radioGroup2' /><br />
RadioGroup2Button 3: <input type='radio' name='radioGroup2' /><br />
Please help 请帮忙
Is it something like this you want? 你想要这样的东西吗?
$("input:radio").on("mousedown", function() {
var name = $(this).attr("name");
if ($("input:radio[name='" + name + "']:checked").length == 1 && $(this).is(":not(:checked)")) {
$("input:radio[name='" + name + "']").removeClass("PrevChecked");
$("input:radio[name='" + name + "']:checked").addClass("PrevChecked");
}
});
Demo 演示
$("input:radio").on("mousedown", function() { var name = $(this).attr("name"); if ($("input:radio[name='" + name + "']:checked").length == 1 && $(this).is(":not(:checked)")) { $("input:radio[name='" + name + "']").removeClass("PrevChecked"); $("input:radio[name='" + name + "']:checked").addClass("PrevChecked"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> RadioGroup1Button 1: <input type='radio' name='radioGroup1' /><br /> RadioGroup1Button 2: <input type='radio' name='radioGroup1' /><br /> RadioGroup2Button 1: <input type='radio' name='radioGroup2' /><br /> RadioGroup2Button 2: <input type='radio' name='radioGroup2' /><br /> RadioGroup2Button 3: <input type='radio' name='radioGroup2' /><br />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.