- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我花了几个小时来解决这个问题,所以当发现这只是错误位置的单引号或其他东西时,我会感到沮丧。
我正在制作一个基本的 HTML 页面来测试基本的验证表单输入。如果我在 validateForm() 函数的开头删除 return false,它会按预期工作。但是,如果它在验证循环内(如下所示),则它不起作用并且表单会提交。 (hello.php 仅输出 hello world)。
此处的所有诊断警报均按预期运行...包括 return-false 之前的警报。 (当我将输入留空时)。
我觉得我错过了一些非常明显的东西,但无法弄清楚它是什么。
<?php
echo <<<END
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<title>Test</title>
<script type="text/javascript">
$(document).ready(function(){
$("input").focus(function(){
$(this).css("background-color","#cccccc");
});
$("input").blur(function(){
$(this).css("background-color","#ffffff");
});
});
function validateForm() {
alert("Validating");
$("form[name=queryForm]").find(":input[type=text]").each(function() {
alert("Checking " + $(this).attr('name'));
if($(this).val()==null || $(this).val()=="") {
$(this).val('REQUIRED');
$(this).css("background-color","#ffbbbb");
alert($(this).attr('name') + ' must not be left blank.');
return false;
}
}); // end of the each function
}
</script>
</head>
<body>
<form name="queryForm" action="hello.php" onsubmit="return validateForm();" method="post">
<input type="text" name="First Name"><br>
<input type="text" name="Last Name">
<input type="submit" value="Submit">
</form>
</body>
</html>
END;
?>
最佳答案
在 .each() 中使用 return 不会得到所需的结果。
试试这个:
function validateForm() {
alert("Validating");
var result = true;
$("form[name=queryForm]").find(":input[type=text]").each(function() {
alert("Checking " + $(this).attr('name'));
if($(this).val()==null || $(this).val()=="") {
$(this).val('REQUIRED');
$(this).css("background-color","#ffbbbb");
alert($(this).attr('name') + ' must not be left blank.');
result =false;
return false; //break the each()
}
}); // end of the each function
return result;
}
关于javascript - 返回 false 被 onsubmit 忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26289649/
在下面的代码中,看起来 onsubmit && !onsubmit() 试图防止意外提交表单两次。 Search: function submitform() { if(documen
如果 onsubmit 事件和 onsubmit 属性都定义在同一个表单上会怎样? 让我们举个例子:- ... function validate() { alert("
标题基本上说明了一切。 我遇到的错误: document.forms[0].onsubmit = validateForm; 这是在我的 .js 文件的最顶部。这重要吗? Unable to set
下面的示例是否意味着表单将提交两次? function submitForm(){ document.myForm.submit(); } 我有一个错误,有时会记录每条记录中的两个。我怀疑它是
这个问题不言自明。我不明白 return 在以下代码中做了什么: 最佳答案 您需要 return 以便将 true/false 传递给表单的提交事件(该事件会查找它并在它得到 false 时阻止提交
两者有什么区别 onsubmit="javascript:return false" 和 onsubmit="return false" ? 最佳答案 第一个示例以 label 开头(可以链接到 br
用于仅添加带有按钮的onclick事件并收集数据,甚至不需要表单元素,而是使用HTML5您可以利用错误检查和用户提交表单时显示的默认消息。 由于类型是email并且存在require属性,浏览器将检
我是一个 super React-redux 初学者,刚刚开始从事我的第一个个人项目。我正在尝试使用 Redex-form 构建提交表单,但遇到了第一个我无法克服的错误...这是我唯一可以提出问题的地
我想创建一个简单的表单,它接受一个电子邮件地址,然后将其添加到我们的数据库中。我选择了 React Forms,因为它促进了整个开发过程并减少了时间。 但是,当我尝试 POST 我的表单时出现此错误:
我有以下 Angular 7 表单: ngOnInit() { this.organizationId = 1; // Used for testing this.form = this.fo
所以我有这个代码。 class Validationator { constructor() { this.initValidation() } ini
我在 coldfusion 页面中有一个表单。点击插入表格的保存按钮后,我需要在选择框中使用表格的新值重新加载页面。虽然页面在提交后刷新,但我无法在下拉列表中看到新插入的值。只有在手动刷新页面后我才能
我有以下 JavaScript 代码。 window.onload = function init() { var appForm = document.getElementById("applica
我正在使用Sinon来监视该方法,但是当我尝试模拟onSubmit时,我正在监视的方法似乎根本没有被调用在 form 标记上。 这是一个JSFiddle . 最佳答案 首先,这一行: React.ad
我的页面有嵌套记录,并且每个嵌套记录在名为 “count” 的 div 中的每条记录都有点击总数。我想将 view 类的按钮的每个 div onclick 总数加 1。目前我正在实现的是 onclic
在 mi 验证表单中,我有两个输入字段,用于编写电子邮件并进行确认。在提交信息之前,需要进行两次确认:1-电子邮件必须看起来是一封电子邮件,2-电子邮件地址一必须与电子邮件地址二匹配。我可以使用两个单
这是我的 html 代码,我希望当我提交时,会出现一条警报消息。 这是js代码: $(document).ready(function () { $('#reportForm').submi
验证函数 function validateForm(){ var els; els = document.getElemen
我的自定义组件有 onChange 事件,该事件运行得很好,但是当我尝试 onSubmit 时,它不起作用。警报不显示。 目前,我的数据提供程序从输入中获取除自定义组件之外的所有值,我应该做什么?代码
提交表单后,我的 react 应用程序没有重定向到仪表板页面。我尝试使用 react-router-dom Redirect 功能,但仍然没有成功。然后我选择加入 BrowserHistory.pus
我是一名优秀的程序员,十分优秀!