I have radio buttons in HTML like this:
1
-
I know this is old and that this is a little off topic, but supposing you wanted to uncheck only specific radio buttons in a collection:
$("#go").click(function(){
$("input[name='correctAnswer']").each(function(){
if($(this).val() !== "1"){
$(this).prop("checked",false);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="radio1" type="radio" name="correctAnswer" value="1">1</input>
<input id="radio2" type="radio" name="correctAnswer" value="2">2</input>
<input id="radio3" type="radio" name="correctAnswer" value="3">3</input>
<input id="radio4" type="radio" name="correctAnswer" value="4">4</input>
<input type="button" id="go" value="go">
And if you are dealing with a radiobutton list, you can use the :checked selector to get just the one you want.
$("#go").click(function(){
$("input[name='correctAnswer']:checked").prop("checked",false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="radio1" type="radio" name="correctAnswer" value="1">1</input>
<input id="radio2" type="radio" name="correctAnswer" value="2">2</input>
<input id="radio3" type="radio" name="correctAnswer" value="3">3</input>
<input id="radio4" type="radio" name="correctAnswer" value="4">4</input>
<input type="button" id="go" value="go">
讨论(0)
-
Although prop changed the checked status but change also show that in the form.
$('input[name="correctAnswer"]').prop('checked', false).change();
讨论(0)
-
Your problem is that the attribute selector doesn't start with a @
.
Try this:
$('input[name="correctAnswer"]').attr('checked', false);
讨论(0)
-
If you want to clear all radio buttons in the DOM:
$('input[type=radio]').prop('checked',false);
讨论(0)
-
<div id="radio">
<input type="radio" id="radio1" name="radio"/><label for="radio1">Bar Chart</label>
<input type="radio" id="radio2" name="radio"/><label for="radio2">Pie Chart</label>
<input type="radio" id="radio3" name="radio"/><label for="radio3">Datapoint Chart</label>
</div>
$('#radio input').removeAttr('checked');
// Refresh the jQuery UI buttonset.
$( "#radio" ).buttonset('refresh');
讨论(0)
-
Finally after a lot of tests, I think the most convenient and efficient way to preset is:
var presetValue = "black";
$("input[name=correctAnswer]").filter("[value=" + presetValue + "]").prop("checked",true);
$("input[name=correctAnswer]").button( "refresh" );//JQuery UI only
The refresh is required with the JQueryUI object.
Retrieving the value is easy :
alert($('input[name=correctAnswer]:checked').val())
Tested with JQuery 1.6.1, JQuery UI 1.8.
讨论(0)
- 热议问题