问题描述
我建立了一个网站,展示用于投资的金融网站.所以我给客户选择答案问卷,以给他们投资组合建议.问卷从数据库中调用并通过查询显示.因此,为了确定单选按钮的组以检查在每个问题中选择了哪个单选按钮,我给每个组指定了不同的名称.(名称为@ row.ID:问题1的输入为1,问题2的输入为2 .....)
但是当我尝试通过javascript中的函数获取元素时,它不起作用!!! (我知道,因为我尝试通过document.write()在屏幕上写列表"的长度,但有时它为零,有时什么也不显示!!)
HTML代码是这样的:
I build a website that demonstrate finnacial website for investments. so I give to the clients option to answer questionnaire befor advice to them portfolio. the questionnaire is called from the database and shown by query. so to identify group of radiobuttons for checking which of them choosen in every question I gave different names to every group.(the name is @row.ID: 1 to the inputs of question 1, 2 to question 2.....)
but when I tryed to get the elements by function in javascript, it''s not work!!! (I know becouse I tryed to write on screen the length of the "list" by document.write() but sometimes it''s zero and sometimes show nothing!!!)
the HTML code is like that:
<form method="post" dir="rtl">
<fieldset class="field">
<legend style="direction: rtl"></legend>
<br>
<div style="direction: rtl" id="questionList">
@foreach(var row in db.Query(displayQuestion))
{
<h3>/question number @counter: </h3>
<h3>@row.subject: </h3>
<h3>@row.describe</h3>
<input type="radio" id="answer1" name="@row.ID" value="@row.ID"
önchange="chekRadioToAnswer(@row.ID,'1')" >@row.ans1<br>
<input type="radio" id="answer2" name="@row.ID" value="@row.ID"
önchange="chekRadioToAnswer(@row.ID,'2')">@row.ans2<br>
<input type="radio" id="answer3" name="@row.ID" value="@row.ID"
önchange="chekRadioToAnswer(@row.ID,'3')">@row.ans3<br>
<input type="radio" id="answer4" name="@row.ID" value="@row.ID"
önchange="chekRadioToAnswer(@row.ID,'4')">@row.ans4<br>
if(row.ans5 != null)
{
<input type="radio" id="answer5" name="@row.ID" value="@row.ID"
önchange="chekRadioToAnswer(@row.ID,'5')">@row.ans5<br>
}
<br>
counter+=1;
}
<input type="hidden" id="hiddenAnswers" name="hiddenAnswers" value="0">
<input type="hidden" id="hiddenAns1" name="hiddenAns1" value="0">
<input type="hidden" id="hiddenAns2" name="hiddenAns2" value="0">
<input type="hidden" id="hiddenAns3" name="hiddenAns3" value="0">
<input type="hidden" id="hiddenAns4" name="hiddenAns4" value="0">
<input type="hidden" id="hiddenAns5" name="hiddenAns5" value="0">
<input type="hidden" id="hiddenAns6" name="hiddenAns6" value="0">
<br></br>
<input type="submit" value="submit questionnaire" class="submit" />
<input type="button" style=" float: left" value="skip questionnaire" önclick="ReDirect()" >
<br></br>
</div>
</fieldset>
</form>
<script type="text/javascript">
var signAnswers = new Array(0, 0, 0, 0);
var grades = [[0, 5, 10, 15], [15, 12, 10, 5, 0], [0, 5, 10, 15, 20], [0, 0, 20, 30]];
var answers = new Array(0, 0, 0, 0, 0, 0);
function chekRadioToAnswer(numQuestion, numAnswer) {
if (numQuestion != 1 && numQuestion != 6) {
var list = document.getElementByName(numQuestion);
for (i = 0; i < list.length; i++) {
if (list[i].value == numQuestion) {
if (list[i].checked) {
if (signAnswers[numQuestion - 2] == 0) {
var a = document.getElementById("hiddenAnswers").setAttribute("value", value + grades[numQuestion - 2][i]);
signAnswers[numQuestion - 2] = grades[numQuestion - 2][i];
}
else {
document.getElementById("hiddenAnswers").setAttribute("value", value - signAnswers[numQuestion - 2]);
document.getElementById("hiddenAnswers").setAttribute("value", value + grades[numQuestion - 2][i]);
signAnswers[numQuestion - 2] = grades[numQuestion - 2][i];
}
break;
}
}
}
}
}
</script>
推荐答案
这篇关于getElementByName不适用于动态创建的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!