2012-02-03 42 views
0
$('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

讓我們說你的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); 
}); 

通過做t他爲每個人添加一個點擊事件,然後用qid類挑選該列的文本並構建您的消息。您可以在此嘗試:http://jsfiddle.net/franky/f956k/

相關問題