我想当表单的输入字段为空时,它会显示警报并停止表单提交。现在我的代码问题是它只显示警报但不会停止表单提交。这是我的html输入字段和脚本:
function required() {
var empt = document.forms["me_validate"]["insert_vender_emails"].value;
if (empt == "") {
alert("Please fill out the vendor email field");
}
}
<form enctype="multipart/form-data" id="validate-me" method="post" class="form-horizontal" action="phpcode_class.php" role="form" name="me_validate" onsubmit="return required()">
<textarea type="text" id="tags" name="insert_vender_emails" class="form-control" placeholder="">
<?php if($n_vendors!='' ) { echo $n_vendors; } else { echo $rfq[ 'vender_emails']; } ?>
</textarea>
</form>
我真的陷入了困境。请帮我摆脱这个......提前谢谢
答案 0 :(得分:0)
<html><head>
<script type="text/javascript">
function submit_form() {
if (conditions) {
document.forms['myform'].submit();
}
else {
alert("Validation Failed;");
}
}
</script>
</head><body>
…
<input type="button" name="submit" value="submit" onclick="submit_form();"/>
</body></html>
答案 1 :(得分:0)
试试这个,
<script>
function required()
{
var empt = document.forms["me_validate"] ["insert_vender_emails"].value;
if (empt == "")
{
alert("Please fill out the vendor email field");
return false;
}
}
</script>
答案 2 :(得分:0)
请放回假;警告后的消息。请查看以下代码。
<script>
function required()
{
var empt = document.forms["me_validate"]["insert_vender_emails"].value;
if (empt == "")
{
alert("Please fill out the vendor email field");
return false;
}
}
</script>
答案 3 :(得分:0)
当您提交表单时,它将重新加载所有页面。
使用 return false
来克服这种情况。
<script>
function required()
{
var empt = document.forms["me_validate"]["insert_vender_emails"].value;
if (empt == "")
{
alert("Please fill out the vendor email field");
return false;
}
}
</script>
答案 4 :(得分:0)
可能你没有正确选择元素,
function required()
{
var empt = document.getElementById("tags").value;
if (empt == "")
{
alert("Please fill out the vendor email field");
return false;
}
}
答案 5 :(得分:0)
在提示弹出消息后,尝试将某些内容返回到表单。
<script>
function required()
{
var empt = document.forms["me_validate"]["insert_vender_emails"].value;
if (empt == "")
{
alert("Please fill out the vendor email field");
return false;
}
return (true);
}
</script>
答案 6 :(得分:0)
使用新的HTML5 required
属性
<textarea type="text" id="tags" name="insert_vender_emails" class="form-control" placeholder="" required></textarea>
http://www.w3schools.com/tags/att_input_required.asp
请记住,通过这种方式,您无法验证输入数据。验证仅存在于应用程序的服务器端。看看this发布的帖子
答案 7 :(得分:0)
致电required
function
onsubmit()
,如果textarea
为空,则返回
<form enctype="multipart/form-data" id="validate-me" method="post" class="form-horizontal" action="phpcode_class.php" role="form" name="me_validate" onsubmit="return required()">
<textarea type="text" id="tags" name="insert_vender_emails" class="form-control" placeholder="" ><?php if($n_vendors!='') { echo $n_vendors; } else { echo $rfq['vender_emails']; } ?></textarea>
<input type="submit" value="submit" name="submit" onsubmit="required()">
</form>
<script>
function required()
{
var empt = document.forms["me_validate"]["insert_vender_emails"].value;
if (empt == "")
{
alert("Please fill out the vendor email field");
return false;
}
}
</script>
答案 8 :(得分:0)
您可以使用HTML5“必需”验证:
ENSession.sharedSession().findNotesWithSearch(nil, inNotebook: nil, orScope: .None, sortOrder: .RecentlyUpdated, maxResults: 100, completion: {(results:[AnyObject]!, error:NSError?) in
self.noteList = results
});
OR
简单地写“return false;”在您所需的功能中发出警告信息后:
<textarea type="text" id="tags" name="insert_vender_emails" class="form-control" placeholder="" required>
<?php if($n_vendors!='' ) { echo $n_vendors; } else { echo $rfq[ 'vender_emails']; } ?>
</textarea>