它不是以字符串形式显示数字

时间:2012-02-03 12:56:48

标签: jquery

我使用下面的代码来说明发生错误的表行(问题编号)。

 var _qid = $("td.qid", this).text();
 var _msg = "You have errors on Question Number: " + _qid + "\n";

上面的代码进入下面的验证函数:

 function validation() {

     var _qid = $("td.qid", this).text();
     var _msg = "You have errors on Question Number: " + _qid + "\n";

    alertValidation= "";
        // Note, this is just so it's declared...


    $(".textAreaQuestion").each(function() {
        if (!this.value || this.value.length < 5) {
            alertValidation += "\nYou have not entered a valid Question\n";
        }

            if(alertValidation != ""){ 
                return false;//Stop the each loop 
            } 
    });

        $(".numberAnswerTxtRow").each(function() {
       var currenttotal = $(this).closest('.optionAndAnswer').find('.answerBtnsOn') .length; 

        if (!this.value) {
            alertValidation += "\nPlease Enter in the Number of Answers you Require for this question\n";
        }

    if(alertValidation != ""){ 
                return false;//Stop the each loop 
            } 

    });

问题是它没有显示数字。它显示“您在问题编号上有错误:”但它没有在分号后显示问题编号。如何才能显示该号码?

下面是添加每个表行并在添加的行中添加问题编号列的代码(表行1是问题编号1,表行2是问题编号2等):

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); 

} 

以下是添加的行添加到的html:

<table id="qandatbl" align="center">
<thead>
<tr>
    <th class="qid">Question No</th>
</tr>
</thead>
<tbody></tbody>
</table>

myclickHander()函数检查验证,如下所示:

function myClickHandler(){
     if(validation()){
                showConfirm();
     }
}

从这样的提交按钮调用myClickHandler()函数:

<p><input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" onClick="myClickHandler(); return false;" /></p>

1 个答案:

答案 0 :(得分:0)

分配qid时,您的上下文丢失了。 它仅在

的上下文中有效
$("tr"). Click(function (){ 
    // the context is valid here
}) 

否则你的代码不知道你指的是哪个。

如果您通过按钮触发事件,则更相关。 你的HTML页面

<table border="1" width="200">
    <tr>
        <td class='qid'>1</td> 
        <td><input id="question1" class="question"  value="This is valid"/></td>
    </tr>
    <tr>
        <td class='qid'>2</td>
        <td><input id="question2" class="question" value="not"/></td>
    </tr>
    <tr>
        <td class='qid'>3</td>
        <td><input id="question3" class="question" value="This is also valid"/></td>
    </tr>
</table>
<button onclick="test()">Test</button>

然后是代码

function test(){

    $(".question").each(function(i,e){
        if(e.value.length < 5){
            console.log(e.id);
            var _qid = $("#"+e.id).parent().parent().eq(0).text();
            //console.log(_qid)
            alert("You have errors on Question Number: " + _qid);
        }
    }) 
}

我也把它放在jsFiddle:http://jsfiddle.net/franky/bAbb9/

我希望这有所帮助。