$('td.qid').text().
上面的代码将确定警报所引用的行。以下是警报的内容:
You have not entered a valid Question
Please Enter in the Number of Answers you Require for this question
Please enter in a figure for Number of Marks for this Question
我希望警报如下所示:
You have errors on question number: 1
You have not entered a valid Question
Please Enter in the Number of Answers you Require for this question
Please enter in a figure for Number of Marks for this Question
我的问题是,我在哪里可以在下面的函数中放置“您在问题编号上有错误”和代码$('td.qid').text()
,以便能够在顶部显示“您在问题编号上有错误:1”警报?
以下是验证功能:
var qnum = 1;
function insertQuestion(form) {
var $tbody = $('#qandatbl > tbody');
var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
var $qid = $("<td class='qid'>" + qnum + "</td>");
$tr.append($qid);
$tbody.append($tr);
$(form).find('.numberOfQuestions').val(qnum);
++qnum;
$("#questionNum").text(qnum);
}
由于
答案 0 :(得分:0)
假设您的html页面如下所示:
> <html> <body>
> <table border="1" width="200">
> <tr>
> <td class='qid'>1</td>
> <td>bla bla</td>
> </tr>
> <tr>
> <td class='qid'>2</td>
> <td>bla bla</td>
> </tr>
> <tr>
> <td class='qid'>3</td>
> <td>bla bla</td>
> </tr>
> </table>
> </body> </html>
然后你添加一个像这样的jQuery代码:
$("tr").click(function(e){
var _qid = $("td.qid", this).text();
var _msg = "You have errors on question number:" + _qid ;
alert(_msg );
});
通过这样做,您每次都会添加一个click事件,然后使用类qid选择列的文本并构建您的消息。
你可以在这里试试:http://jsfiddle.net/franky/f956k/