Use jquery to just set radio buttons that are currently a certain value
I've an HTML page with a number of tables, one for each user in the database. The first four columns of each table are radio buttons, with the values: G, A, X and blank respectively.
What I want to do is set the radio button to e.g. 'A' for all rows that are currently blank.
Here was where I started:
var st='A';
$('#user123 input').val([st]);
This works, but it also sets to A the rows that were already 'G' or 'X'.
Here is what I've tried that didn't work:
$('#user123 input[value=]').siblings().val([st]);
(nothing gets set)
$('#user123 input[value=]').parent().children().val([st]);
(the blank radio button gets cleared, none get set)
$('#user123 input[value=]').val([st]);
(the blank radio button gets cleared, none get set)
$('#user123 input').not('[value=G]').val([st]);
(planning to add exceptions for A and X later, but it already doesn't work - all radio buttons get set to 'A', not just the blank ones)
I'm really close aren't I? But I just cannot find the magic incantation I need!
Here is a cut-down version of the table HTML:
<table id="user123">
<tr>
<td><input name="r520" value="G" type="radio"></td>
<td><input name="r520" value="A" type="radio"></td>
<td><input name="r520" value="X" type="radio"></td>
<td><input name="r520" value="" type="radio" checked></td>
<td>2011-06-01</td>
</tr>
<tr>
<td><input name="r767" value="G" type="radio"></td>
<td><input name="r767" value="A" type="radio"></td>
<td><input name="r767" value="X" type="radio"></td>
<td><input name="r767" value="" type="radio" checked></td>
<td>2011-06-03</td>
</tr>
</table>
UPDATE Thanks for replies; here is the s开发者_StackOverflowolution I went with (mainly inspired by michelpm's):
var st='A';
$('#user123 tr').each(function() {
if($("input:radio[value=]:checked",this).length){
$("input:radio[value="+st+"]",this).attr("checked",true);
}
});
If by blank you meant no radio is checked in that row:
http://jsfiddle.net/michelpm/GzPrd/
$("#user123 tr").each(function() {
if($(":radio:checked", this).length == 0) {
$(":radio[value=A]", this).attr("checked", true);
}
});
EDIT:
If you want so bad to do that in one line this one may do the trick...
http://jsfiddle.net/michelpm/GzPrd/2/
function select(value) {
return function() {
$(":radio[name=" + this.name + "][value=" + value + "]")
.attr("checked", true);
};
}
$("#user123 :radio:not([value]):checked").each(select('A'));
First of all, if you are going to use this code on multiple tables/rows on multiple users, you should probably make it a bit more dynamic by adding a class to the table. Consider the following:
jQuery(document).ready(function($) {
var value_to_check = 'A';
$('.user-table tr').each(function() {
//is blank checked?
if( $(this).find( 'input[value=]' ).is(':checked') )
$(this).find( 'input[value=' + value_to_check + ']' ).attr( 'checked', 'checked' );
});
});
This code assumes that the table has the class "user-table" instead of just an ID such as "#user123". It then iterates through each row, since your example HTML shows the need for more than 1 row. It then checks each row to see if the radio button with the "" value is checked. If so, then it checks the row you have predefined, which in this case is "A".
Not sure I understand the question, but I think you want to do this:
If a blank row is checked, change it to make the A the checked item. If that is right, you could do this.
if($('#user123 tr input[value=""]')){
$('#user123 tr input[value=A]').attr("checked", true);
}
Example: http://jsfiddle.net/jasongennaro/vbyS6/
Also, you wrote
so you think there is no snappy jquery one-liner for this case?
You could smush this into one line:
if($('#user123 tr input[value=""]')){$('#user123 tr input[value=A]').attr("checked", true);}
精彩评论