Javascript按钮单击document.write函数 [英] Javascript button click document.write function
本文介绍了Javascript按钮单击document.write函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<html>
<head>
<meta charset="utf-8">
<title>C S</title>
<script type="text/javascript">
function vals()
{
if(rbnNumber.value=4)
{
document.write("You selected Tunisia")
}
}
</script>
</head>
<body>
<table align = "center">
<tr>
<td>
<input type="radio" name="rbnNumber" value="1" />Tasmania<br/>
</td>
</tr>
<tr>
<td>
<input type="radio" name="rbnNumber" value="2" /> Nicaragua<br/>
</td>
</tr>
<tr>
<td>
<input type="radio" name="rbnNumber" value="4" /> Tunisia<br/>
</td>
</tr>
<tr>
<td>
<input type="radio" name="rbnNumber" value="5" /> Algeria<br/>
</td>
</tr>
</table>
<table align = "center">
<input type="button" id="vals" Value="Vals" onclick="vals()"/>
</table>
</body>
</html>
我尝试过:
document.write不会写入所选单选按钮的值
What I have tried:
document.write does not writes the value of selected radio button
推荐答案
function vals()
{
// use getElementsByName to get all the radio buttons
var radios = document.getElementsByName("rbnNumber");
// loop through each one
for (var i = 0; i < radios.length; i++) {
var r = radios[i];
// check the value is 4 and that the radio button is selected
// note that you use a double equals ("==") to compare
if (r.value == "4" && r.checked) {
document.write("You selected Tunisia")
}
}
}
你需要使用== not =
You need to use == not =
if(rbnNumber.value=='4')
此外, value是这个上下文中的一个字符串,不是一个整数,虽然我敢说JavaScript的隐式转换可以解决这个问题。
Also, the value is a string in this context, not an integer, though I dare say JavaScript's implicit conversion can take care of that.
document.write不打印你选择的Tasmania
the document.write does not prints you selected Tasmania
<html>
<head>
<title>Javascript Counts</title>
<script type="text/javascript">
function vals()
{
var radios = document.getElementsByName("rbnNumber");
for (var i = 0; i < radios.length; i++) {
var r = radios[i];
if (r.value == "4" && r.checked) {
document.write("You selected Tasmania")
}
</script>
</head>
<body>
<table align = "center">
<tr>
<td>
<a href = "some.html">Previous
</td>
<td>
What is the Capital of France
</td>
<td>
<a href = "some.html">Next
</td>
</tr>
</table>
<table align = "center">
<tr>
<td>
<input type="radio" name="rbnNumber" value="4" />Tasmania<br/>
</td>
</tr>
<tr>
<td>
<input type="radio" name="rbnNumber" value="Nicaragua:which is not the correct answer: Try Again" /> Nicaragua<br/>
</td>
</tr>
<tr>
<td>
<input type="radio" name="rbnNumber" value="Tunisia:which is not the correct answer: Try Again" /> Tunisia<br/>
</td>
</tr>
<tr>
<td>
<input type="radio" name="rbnNumber" value="Algeria:which is not the correct answer: Try Again" /> Algeria<br/>
</td>
</tr>
</table>
<table align = "center">
<tr>
<td align="center">
<input type="button" id="btnGetValue" onclick="vals()" Value="Get Value" />
<p></p>
</td>
</tr>
<tr>
</tr>
</table>
</body>
</html>
这篇关于Javascript按钮单击document.write函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文