我有一個textarea,用戶在其中輸入問題,然後單擊「添加問題」按鈕提交問題並在問題中添加問題表作爲新的一行。如果出現警告框,我不希望將一行添加到表中
雖然用戶可能會錯誤地鍵入他們的問題,但它帶有必要的警報。當警報出現時,它會工作,但它也會在表格中添加問題,我需要包含哪些內容,以便如果警告框出現,它不會添加問題?
Javascript代碼:下面
function insertQuestion(form)
{
var row = document.createElement("tr");
var cell, input;
var alertErrors = "";
cell = document.createElement("td");
cell.className = "question";
input = document.createElement("textarea");
input.name = "question_" + qnum;
input.value = form.questionText.value;
cell.appendChild(input);
row.appendChild(cell);
if (input.value == ""){
alertErrors += "\nYou have not entered a valid Question";
}else if (input.value.length < 5){
alertErrors += "\nYou have not entered a valid Question";
}
}
問題文本區域,並添加按鈕:
<form id="enter" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validateForm(this);" >
<table id="middleDetails" border="1">
<tr>
<th class="tblheading" colspan="2">SESSION DETAILS</th>
</tr>
<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>
<tr>
<th colspan="2">
<input name="addQuestion" type="button" value="Add Question" onClick="insertQuestion(this.form)" />
</th>
</tr>
</table>
表哪些商店添加以下qustions:
<hr/>
<table id="qandatbl" border="1">
<tr>
<th class="qid">Question No</th>
<th class="question">Question</th>
<th class="weight">Weight</th>
<th class="answer">Answer</th>
</tr>
</table>
<input type="submit" value="Submit questions to database" />
<input type="hidden" name="numberOfQuestions" value="0" />
</form>
你能發佈[JS小提琴(http://jsfiddle.net/),或相似,演示? –
我試圖把它放在jsFiddle中,但警告框在JsFiddle上不起作用,但它在瀏覽器上工作 – BruceyBandit