How to update radio value using JQuery
I have this HTML code for radio stations:
<input type='radio' name='a_27' value='Yes' id='a_27_0' />
<input type='radio' name='a_27' value='No' id='a_27_1' />
I am trying to set the selected radio value using this code:
var field="a_" + this.id;
$('[name="' + field + '"]').val(this.value);
console.log("name is " + field + ", val is " + this.value);
However, this doesn't work, and nothing happens. Here's the output from the Firebug console that appears after the third line:
name - a_27, val - Yes
Any ideas?
I would prefer a method that would also work for <select>s
, so I would not need to write additional / separate code for radios and choose.
Edit: weird problem. I noticed that although my html code gives a different meaning (yes / no), in firebug it shows that radios have a yes. If I choose no and hit save, the javascript function still gets "yes" instead of no. Am I doing something wrong?
Edit 2: full function:
function processMultiOptAnswers()
{
$.each(multiOpts,function()
{
var field="a_" + this.id;
console.log("name is " + field + ", val is " + this.value);
$('[name="' + field + '"]').val(this.value);
}
);
}
source to share
your log should be if this.value
different.
$('[name="' + field + '"]').val(this.value);
console.log("name is " + field + ", val is " + $('[name="' + field + '"]').val());
To make him the chosen one
$('[name="' + field + '"]').attr("checked", "checked");
I haven't tested this, but you might need to remove this attribute from others.
source to share