我的应用程序适用于除Internet Explorer之外的所有浏览器

时间:2011-11-30 10:45:34

标签: javascript html internet-explorer browser html-table

我在这里有一个小问题:

我有一个函数,它会在用户输入textarea内的问题后添加行,然后提交问题,如果问题有效,那么它会在表格中添加一行,问题在行内。< / p>

这适用于所有主流浏览器,除了一个(惊喜,它是Internet Explorer)。在Internet Explorer中,当我提交问题时,它不会添加一行,在我单击提交按钮后它不会添加一行。我对该问题的验证有效,但在问题有效时不会添加该行。

为什么我的功能在Internet Explorer中不起作用?

以下是我的代码:

使用Javascript:

function insertQuestion(form) {   

    var row = document.createElement("tr");
    var cell,
        input,
        alertErrors = "",
        // Note, this is just so it's declared...
        qnum = 1;

    // Note, I'm using the .value instead of copying it
    // Also note the .length on the second check
    if (form.questionText.value == ""){
        alertErrors += "\nYou have not entered a valid Question";
    } else if (form.questionText.value.length < 5){
        alertErrors += "\nYou have not entered a valid Question";
    }

    // Stop execution with a return
    if (alertErrors != "") {
        alert(alertErrors);
        return;
    }

     cell = document.createElement("td"),
     input = document.createElement("textarea");
    cell.className = "question";
    input.name = "question_" + qnum;
    input.value = form.questionText.value;
    cell.appendChild(input);
    row.appendChild(cell);


     document.getElementById("qandatbl").appendChild(row);
     form.numberOfQuestions.value = qnum;

     ++qnum;
     document.getElementById("questionNum").innerHTML = qnum;
     form.questionText.value = "";
}

Html table和textarea并提交问题按钮:

<form id="enter" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validateForm(this);" >

//below is question textarea and submit button

<table id="middleDetails" border="1">
<tr>
    <th colspan="2">
        Question Number <span id="questionNum">1</span>
    </th>
</tr>
<tr>
    <td>Question:</td> 
    <td>
        <textarea rows="5" cols="40" name="questionText"></textarea>
    </td>
   <tr>
    <th colspan="2">
        <input name="addQuestion" type="button" value="Add Question" onClick="insertQuestion(this.form)" />
    </th>
</tr>
</table>

<hr/>

//below is where the rows get added after question is submitted and valid

<table id="qandatbl" border="1">
<tr>
    <th class="qid">Question No</th>
    <th class="question">Question</th>
</tr>
</table>
<input type="submit" value="Submit questions to database" />
<input type="hidden" name="numberOfQuestions" value="0" />
</form> 

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用insertRow()insertCell() JavaScript函数?

有关动态构建表的建议,请参阅this msdn page