【发布时间】:2013-01-26 19:02:36
【问题描述】:
我正在寻找能够以最简洁的方式回答简单的是或否的问题并测试已选择的答案。
例如,在这种情况下,如果“是”,我希望它将“继续”的值返回到指定区域(第一个有问题的 3 列表,第二个有单选按钮,我希望它更新并在第三个中显示答案)。
到目前为止,我的 JS 代码:
<script type="text/javascript">
var answer = 'place-holder';
var user_input;
function checkForm()
{
var substanceab = document.getElementById('Sub');
for (i = 0; i < substanceab.length; i++)
{
if(substanceab.length[i].checked)
{
user_input = substanceab.length[i].value;
}
}
if (user_input ="yes")
{
return answer = "Continue";
}
else if (user_input ="no")
{
return answer = "Provide Alternate Referral";
}
else
{
return;
}
};
function writeAns()
{
document.getElementById('answerwrite').innerHTML = checkForm[user_input];
};
</script>
和正文(减去实际问题):
<table class="table table-bordered">
<tr>
<td width="58%"><center><strong>Required:</strong></center></td>
<td width="19%"></td>
<td width="23%"><center><u><strong>___________</strong></u></center></td>
</tr>
<tr>
<td> <span class="sub depend"> <strong><i>_________</i> </strong></span></td>
<td> <span class="sub depend ans">
<form name="Substance">
<label class="radio inline">
<input type="radio" value="yes" name="substance" onclick="writeAns()">
yes </label>
<label class="radio inline">
<input type="radio" value="no" name="substance" onclick="writeAns()">
no </label> </form></span></td>
<div id="answerwrite"></div>
<script type="text/javascript">
document.write("<td>" + writeAns() + "</td>")
</script>
</tr></table>
好的,修复了“funk”,但就像我说的,比 javascript 更习惯于 java,语法更严格。我同意,我只使用了 ID 来尝试让它使用不同的方法,但它从来没有这样做过。现在有了一些建议,它只是让我到处都不确定。虽然我同意这最终会转向 jquery,但我不知道如何使用它,因此首先在 javascript 中解决这个问题。
【问题讨论】:
-
document.getElementById 不返回数组,那么为什么要使用 for 循环? materialab.length[i] 应该是什么意思?
-
ID 在您的文档中必须是唯一的。这就是为什么它们被称为 ID。
-
您的标记是非法的,因为您有多个具有相同 id 的元素...我有相同的名称可能是您打算用于单选按钮的名称。
-
和 checkForm[user_input];应该是什么意思?我认为您想使用 '(' 而不是 '[' 因为 checkForm 是一个函数并且您想调用它。
-
dherbolt,你有魔眼,这是完成我的代码的一个严重问题。一旦我得到了改变,一切都会按照我的需要工作。