我在下面有一个变量问题:
function validation() {
var context = $('#optionAndAnswer');
var currenttotal = context.find('.answerBtnsOn').length;
alertValidation= "";
// Note, this is just so it's declared...
$(".numberAnswerTxtRow").each(function() {
if (!this.value) {
alertValidation += "\nPlease Enter in the Number of Answers you Require for this question\n";
}
else if (currenttotal > $(this).val()){
alertValidation += "\nYou have selected more answers than the required amount\n";
}
else if (currenttotal < $(this).val()) {
alertValidation += "\nYou have selected less answers than the required amount\n";
}
});
}
问题是,对于每一行,它应该查看在.numberAnswerTxtRow文本框中输入的数字,并查看已选择了多少个按钮(.answerBtnsOn.length)。如果选择的按钮多于文本框中输入的数字,则应显示警告您已选择了多于所需的答案。反之亦然,它应该做同样的事情,除了显示消息,说明你选择的答案少于要求。问题是它无法识别在文本框中输入的数字以及为每行选择的按钮数量。这让我相信我需要将var context
和var currenttotal
更改为其他内容,以便识别为每行选择的文本框和按钮。我应该将这些变量更改为什么?
下面是jquery代码,文本框和答案来自:
function insertQuestion(form) {
var context = $('#optionAndAnswer');
var currenttotal = context.find('.answerBtnsOn').length;
var $tbody = $('#qandatbl > tbody');
var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
var $noofanswers = $("<td class='noofanswers'></td>");
var $answer = $("<td class='answer'></td>");
$('.numberAnswerTxt', context).each(function() {
var $this = $(this);
var $noofanswersText = '';
$noofanswersText = $("<input type='text' class='numberAnswerTxtRow answertxt' style='display: block;' onkeyup='numberKeyUp(this)' onkeypress='return isNumberKey(event)' onChange='getButtons()'>").attr('name', $this.attr('name')).attr('value', $this.val())
}
$noofanswers.append($noofanswersText);
});
var $this, i=0, $row, $cell;
$('#optionAndAnswer .answers').each(function() {
$this = $(this);
if(i%7 == 0) {
$row = $("<tr/>").appendTo($answer);
$cell = $("<td/>").appendTo($row);
}
var $newBtn = $("<input class='answerBtnsRow answers' type='button' style='display:%s;' onclick='btnclick(this);' />".replace('%s',$this.is(':visible')?'inline-block':'none')).attr('name', $this.attr('name')).attr('value', $this.val()).attr('class', $this.attr('class'));
$newBtn.appendTo($cell);
i++;
});
$tr.append($answer);
$tr.append($noofanswers);
$tbody.append($tr);
}
将行添加到称为qandatbl:
的表中<table id="qandatbl" align="center">
<thead>
<tr>
<th class="noofanswers">Number of Answers</th>
<th class="answer">Answer</th>
</tr>
</thead>
<tbody></tbody>
</table>
答案 0 :(得分:0)
你的代码看起来很复杂,但仍然缺少某些部分,所以我只能猜测...为什么你不看看变量中有什么。这应该可以帮助你解决这个问题:
var context = $('#optionAndAnswer');
console.log(context);
var currenttotal = context.find('.answerBtnsOn').length;
console.log(context.find('.answerBtnsOn'));