为啥即使没有错误,表单也会出现错误消息?
Posted
技术标签:
【中文标题】为啥即使没有错误,表单也会出现错误消息?【英文标题】:Why the form is error message is coming even after not having the error?为什么即使没有错误,表单也会出现错误消息? 【发布时间】:2014-04-01 15:25:11 【问题描述】:在以下代码中,错误如下:
alert("Fill in the textarea");
即使在填充文本区域后也会出现错误消息。如何避免显示错误消息?
以下是我的 html 代码:
<form name="question_issue_form" id="question_issue_form" action="question_issue.php">
<table class="trnsction_details" cellpadding="5">
<tbody>
<tr>
<td></td>
<td>
<input type="checkbox" name = "que_issue[]" value = "Question is wrong" id ="chkQueWrong">Question is wrong</input>
</td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name = "que_issue[]" value = "Answers are wrong" id ="chkAnsWrong">Answers are wrong</input></td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name = "que_issue[]" value = "Question direction is incorrect" id ="chkDirIncorrect">Question direction is incorrecct</input></td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name = "que_issue[]" value = "Other" id ="chkOther">Other</input></td>
</tr>
<tr>
<td></td>
<td class="set_message" style="display:none;"><textarea name="que_issue_comment" rows="4" cols="25" maxlength="100"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" id="report_question_issue"/></td>
</tr>
</tbody>
</table>
</form>
以下是JS代码:
$(document).ready(function()
$('#chkOther').click(function()
$('.set_message').toggle(this.checked);
);
//This function is use for edit transaction status
$(document).on('click', '#report_question_issue', function(e)
e.preventDefault();
//$.colorbox.close();
//for confirmation that status change
var ans = confirm("Are you sure to report the question issue over?");
if (!ans) //alert("Yes its false");
return false;
var post_url = $('#post_url').val();
var checkbox = document.getElementsByName("que_issue[]");
var checkedAtLeastOne = false;
$('input[type="checkbox"]').each(function()
if ($(this).is(":checked"))
checkedAtLeastOne = true;
return false; //to break from each()
);
if (checkedAtLeastOne)
//alert("In If");
var other = $("#chkOther");
var textfield = $("[name=que_issue_comment]");
if (other.is(":checked"))
if ($.trim($(textfield).text()).length == 0)
//error
alert("Fill in the textarea");
return false;
else
return true;
else
alert("Check at least one checkbox");
return false;
$.ajax(
type: "POST",
url: post_url,
data: $('#question_issue_form').serialize(),
dataType: 'json',
success: function(data)
alert("The values have been inserted");
);
);
);
JS Fiddle链接如下: http://jsfiddle.net/VqmzL/11/
【问题讨论】:
您应该将val()
用于<textarea>
s,而不是text()
。此外,在调用 val()
之前,无需在另一个 jQuery 函数中重新包装 textfield
。至于为什么,看这个帖子:***.com/questions/8854288/val-vs-text-for-textarea
【参考方案1】:
使用textfield.val()
代替textfield.text()
方法。
【讨论】:
以上是关于为啥即使没有错误,表单也会出现错误消息?的主要内容,如果未能解决你的问题,请参考以下文章
为啥即使我设置了 STATIC_ROOT 和 urls.py 也会出现 404 错误
验证 express js 中的表单字段时出现错误消息的问题