admin管理员组

文章数量:1418637

In my file I have javaScript variable name 'testJava'

<script>
var testJava = 'script Test';
</script>

At the bottom of the page I have a checkbox. I need to add 'testJava' variable to the value of the checkbox.

<input type="checkbox" name="website[]" value= "I_WANT_TO_ADD_IT_HERE" />My Test<br />

value = "<script>document.write(testJava);</script>" doesn't work

Can anyone please tell me how to do this?

In my file I have javaScript variable name 'testJava'

<script>
var testJava = 'script Test';
</script>

At the bottom of the page I have a checkbox. I need to add 'testJava' variable to the value of the checkbox.

<input type="checkbox" name="website[]" value= "I_WANT_TO_ADD_IT_HERE" />My Test<br />

value = "<script>document.write(testJava);</script>" doesn't work

Can anyone please tell me how to do this?

Share Improve this question asked Aug 12, 2012 at 19:00 RoshanckRoshanck 2,2909 gold badges43 silver badges56 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 1
<script>
var testJava = 'script Test';
document.getElementById('checkbox_ID').value = testJava;
</script>

You should add an id tag to your <input> element to identify it:

<input type="checkbox" name="website[]" id="thecheckbox" />

Using jQuery, it's quite easy to change the value:

$('#thecheckbox').val(testJava);

To include jQuery, e.g. use the following script tag:

<script src="//ajax.googleapis./ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>

Try this:

var checkBox = document.getElementsByTagName("input")[0];
checkBox.value = testJava;

Note: this must be done within a window.onload event or in a script positioned after the checkbox HTML.

You can't directly do what you're asking, so just set the value after the fact:

<input type=checkbox id=cb1 ...> My Test
<script>
  document.getElementById('cb1').value = testJava;
</script>

Also, Java and JavaScript are not the same thing.

Make sure the <script> is after the <input>, or else that it's in a "load" or "ready" event handler.

<input type="checkbox" name="website[]" id="check_box" value= "I_WANT_TO_ADD_IT_HERE" />My Test<br />

in your script, after the dom ready,

document.getElementById("check_box").value("testJava");

本文标签: How to use javaScript variableinside html checkbox value parameterStack Overflow