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 badges5 Answers
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
版权声明:本文标题:How to use javaScript variable, inside html checkbox value parameter? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745291181a2651804.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论