jQuery 单选按钮组 .change 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12221064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery group of radio buttons .change method
提问by loki
I'm working with a form with a radio button which is part of a set of radios and just one can be selected at a time(no problem with the single selection). One of those radios must fire a code each time it's selected a diferent code when it is unselected (another radio is selected), im using .change
method and it fires the code when you click it, but not when it looses the selection. What i want is to toggle or be able to know when it looses the selection.
我正在使用一个带有单选按钮的表单,它是一组单选的一部分,一次只能选择一个(单选没问题)。其中一个收音机必须在每次选择不同的代码时.change
触发代码,当它被取消选择时(另一个收音机被选中),我使用方法,当你点击它时它会触发代码,但当它失去选择时不会触发代码。我想要的是切换或能够知道它何时失去选择。
I could do it adding code to the other radios .change
method but there are many sets of radios and it would be painful, i'm looking for a one time config.
我可以将代码添加到其他无线电.change
方法中,但是有许多无线电组,这会很痛苦,我正在寻找一次性配置。
Edit
编辑
The markup is made of ASP MVC Razor engine like this:
标记由 ASP MVC Razor 引擎组成,如下所示:
@Html.RadioButtonFor(x => x.SomeModel, new { @class = "special-radio" })
@* Other radios but without the 'specia-radio' class *@
js:
js:
// logging
$(function(){
$('.special-radio').change(function(){
console.log('change');
});
});
The string change only appears when you click the 'special-radio' radio.
字符串更改仅在您单击“特殊收音机”收音机时出现。
采纳答案by maximus ツ
You can do this by using change event on radio group this way,
您可以通过这种方式在广播组上使用更改事件来做到这一点,
check this question,
检查这个问题,
回答by hayk.mart
There is no event triggered when a radio is unchecked, because it would be redundant. Try this instead:
取消选中无线电时不会触发任何事件,因为它是多余的。试试这个:
http://jsfiddle.net/hmartiro/g4YqD/1/
http://jsfiddle.net/hmartiro/g4YqD/1/
It puts a change event on the set of radios and just fires one event if the selected radio is special and another event otherwise.
它在一组收音机上放置一个更改事件,如果所选收音机是特殊的,则只触发一个事件,否则触发另一个事件。
回答by Bob
If adding a class for is not possible. You can use a container with identifier for these radio buttons. This way you can isolate the change event. DEMO FIDDLE
如果添加一个类是不可能的。您可以为这些单选按钮使用带有标识符的容器。这样您就可以隔离更改事件。 演示小提琴
var prev_data;
$('#special_radios input').change(function(){
var me = $(this);
//running function for check
$('#log').html('<p>firing event for check: '+ me.attr('id') +'</p>');
if(prev_data)
uncheck_method(prev_data);
prev_data = me;
});
//@param obj = jquery raidio object
function uncheck_method(obj){
$('#log').append('<p>firing event for uncheck: '+obj.attr('id')+'</p>');
obj.prop('checked', false)
//console.log($('#special_radios input:checked'));
}
?
html
html
<input type="radio" name="zz" id="5"/>
<div id="special_radios">
<input type="radio" name="rx" id="1"/>
<input type="radio" name="ry" id="2" />
<input type="radio" name="rz" id="3"/>
<input type="radio" name="ra" id="4"/>
</div>
<div id="log"></div>
?
回答by Naween Niroshan
Simple solution
简单的解决方案
var newValue; // vale of checked radio button
var oldValue; // value of unchecked radio button
$('.special-radio').change(function(){
oldValue = newValue;
newValue = $(this).val();
});
If oldValue and newValue are same that means hasn't unchecked.
如果 oldValue 和 newValue 相同,则表示尚未取消选中。