【问题标题】:My application works on all browsers except for Internet Explorer我的应用程序适用于除 Internet Explorer 之外的所有浏览器
【发布时间】:2012-01-09 14:42:23
【问题描述】:

我这里有个小问题:

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

这适用于所有主要浏览器,除了一个(令人惊讶的是,它是 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 表格和文本区域以及提交问题的按钮:

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

【问题讨论】:

  • 你在 IE 中遇到任何 JS 错误吗?
  • IE 和操作表格可能很痛苦。我记得曾经有很多隐藏的表格,因为 IE 不允许(没有?)允许您在 JS 中创建新的表格元素。
  • 哪个IE版本?是否存在 JavaScript 错误(状态栏中的黄色警告标志)?文档是否处于标准模式?
  • @Russell:您可以在 IE 中通过 JavaScript 创建新的表格元素,以我的经验一直回到 IE6(我敢打赌,即使 IE5.5 也可以做到)。你不能轻易地通过innerHTML 做到这一点而没有痛苦,但你可以通过createElement / appendChild 做到这一点。 (不过,旧版本不能很好地处理 colspan/rowspan,IIRC。)
  • @MalcolmPickup:您肯定可以找到“关于”框,这样您就可以看到您使用的是什么版本?如果您有菜单,它将在“帮助”菜单上,如果没有,它将在右上角的一个按钮上。如果你有 IE8 或更高版本,你有开发者工具,它允许你在调试器中单步执行你的代码。

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


【解决方案1】:

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

请参阅this msdn page 以获取有关动态构建表的建议

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 2012-12-15
    • 2012-05-04
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多