开发者

Javascript string as variable

ok i have tried eval and window with no success:

I receive part of a radio button name that i then want to use to set that button to checked, below is the function, it works if i replace the square brackets with the name of one of the products so i know the problem is with the stuff in the square brackets, please help, how do i do this?

function load_rights(product)
{
document.form.window[product+"_radio_selection"][0].checked = true;
//also tried - var tempstr = eval(product+'_radio_selection');
开发者_运维知识库//           - document.form.tempstr[0].checked = true;

}

In PHP the radio button is generated:

echo "<input name='".$value."_radio_selection' type='radio' value='yes' />"
ehco "<input name='".$value."_radio_selection' type='radio' checked='checked' value='no' />"

You cnt use the element by id - it does not work with radio buttons, trust me, i spent a whole day the other day learning that.

If i try the element id and add id tags to the html i get a null error, if i use the window i get a null error, but if i put in one of the products like document.form.Admin_radio_selection[0].checked = true; IT WORKs, arrr so annoying ..


Assuming you want a pure Javascript solution, I'd say your best bet is to put IDs on the radio inputs. Then you can use document.getElementById to get a handle to the element.

<input type="radio" id="foo_radio_selection" value="" />Foo<br />
<input type="radio" id="bar_radio_selection" value="" />Bar<br />

function load_rights(product) {
   document.getElementById(product+'_radio_selection').checked = true;   
}

load_rights('bar');

jsfiddle


you could try considering jquery $(':checkbox').filter('name=somename').attr('checked',true);

this is the simplest way of doing this.

or you could also say document.form[0].<<checkboxname>>.checked=true


You need to retrieve the element using its ID with getElementById():

var e = document.getElementById(product+"_radio_selection");
e.checked = true;

In order to let this work you need to add IDs to your radio buttons, as shown by Mark Biek already.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜