- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个问题。我附上了带有示例代码的代码笔。
如果我在输入中输入一个小于 50 的值并立即单击发送按钮,将调用 CHANGE
事件,显示一条错误消息并在输入被覆盖为可能的最小值,但 SUBMIT
事件不会发生。如果我再次输入一个低于 50 的值,CHANGE
事件和 SUBMIT
事件现在都将被触发。
如果我现在给出的值在 50 - 150 的允许范围内,则错误消息将消失,并且只会再次发生 CHANGE
事件。如果我再次给出允许范围内的数字,则会调用 CHANGE
事件和 SUBMIT
事件。
我只需要解决,以便始终调用 submit
事件。
const $form = $(".test-form");
const max = 150;
const min = 50;
$form
.on("change", "input", function () {
console.log("change");
const setVal = $(this).val();
if (setVal < min) {
$(this).val(min);
$(".error-min").show();
}
if (setVal > max) {
$(this).val(max);
$(".error-max").show();
}
if (setVal >= min) {
$(".error-min").hide();
}
if (setVal <= max) {
$(".error-max").hide();
}
})
.on("submit", function (e) {
e.preventDefault();
console.log("submit");
});
input {
display: block;
margin-bottom: 20px;
}
.error {
color: red;
margin-bottom: 20px;
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<form class="test-form">
<input type="number" value="100">
<p class="error error-min">under min val (50)</p>
<p class="error error-max">under max val (150)</p>
<button>SUBMIT</button>
</form>
最佳答案
问题是因为按钮移动了。依次事件,change
事件首先发生,显示红色验证消息,按钮
向下移动。用户单击鼠标然后错过了 button
,因此不会引发 click
事件。
避免这种情况发生的快速修复方法是将 p
元素更改为内联元素,以便它们出现在 input
元素旁边,一个 span
例如:
const $form = $(".test-form");
const max = 150;
const min = 50;
$form.on("change", "input", function() {
const setVal = $(this).val();
console.log('change');
if (setVal < min) {
$(this).val(min);
$(".error-min").show();
}
if (setVal > max) {
$(this).val(max);
$(".error-max").show();
}
if (setVal >= min) {
$(".error-min").hide();
}
if (setVal <= max) {
$(".error-max").hide();
}
}).on("submit", function(e) {
e.preventDefault();
console.log("submit");
});
input {
display: inline-block;
margin-bottom: 20px;
}
button {
display: block;
}
.error {
color: red;
margin-bottom: 20px;
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<form class="test-form">
<input type="number" value="100">
<span class="error error-min">under min val (50)</span>
<span class="error error-max">under max val (150)</span>
<button>SUBMIT</button>
</form>
关于javascript - 当我从表单中显示/隐藏一些 HTML 时,CHANGE 事件阻止 SUBMIT 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73631942/
我创建了一个函数来在两个元素的 style.display 之间切换为“none”或“block”。我在这里遇到的问题是“发送”按钮似乎与“切换”按钮具有相同的行为。这不是我指定它要做的事情。我想知道
当我使用 validate.js 添加表单验证时,其验证正常并显示验证消息。但输入 type="submit"值在操作页面结构中显示两次。 为了说明,我有两个 .cfm 文件,index.cfm 具有
我有一个 s:form,里面有 sj:submit 和 s:submit,sj:submit 工作正常,但 s:submit 按钮不起作用。当我点击 s:submit 按钮时没有任何反应。 我尝试在表
参见http://jsfiddle.net/8KNc7/5/有关我正在讨论的内容的工作示例(目前在 Firefox 15 中进行测试,尚未尝试其他版本)。 我有一个带有 submit 输入和 butt
我有一个包含表单的 HTML 页面。我想做一些字段 "required" 。问题是我没有使用 在我的表单中,我使用 Javascript 函数来提交表单,因为我需要将 Javascript 变量发送到
如果用户提交表单,我想运行特定的 PHP block 。如果我使用带有 name="submit"的提交按钮并且: 我对 javascript 一无所知,我希望代码在用户更改下拉菜单时运行。如果我将
我有一个网站 www.abc.com,其中有一个页面 xyz.php。 xyz.php 采用表单提交事件读取数据并保存在数据库中。 www.abc.com 有一个针对 xyz.php 执行操作的表单。
这个问题在这里已经有了答案: JavaScript post request like a form submit (32 个答案) 关闭 9 年前。 我查看了不同的线程,但找不到合适的解决方案。
我有一个表单提交触发器和一个确认引导框,它显示在单击表单提交按钮上。在 bootbox 中确认如果用户同意我提交表单。我的问题是 onsubmit 在用户说是之前触发,并且当我在 bootbox 中调
我正在使用 JavaScript .submit() 函数提交我的表单。 form.submit(); 但是当我使用 addEventListener 来捕获我的提交事件时,它不起作用。 form.a
编辑:在考虑回答问题之前,请仔细阅读问题的所有内容。我既不是在生产代码中使用内联事件处理程序的可取性,也不是在实现我所引用的文章所 promise 的结果的最佳方法。这是关于Javascript语义和
这是我的形式,对我来说看起来不错。在这种形式中,我放置了这个按钮: 这是它调用的函数: function confirmSubmit() { // get the number of st
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
在表单上添加提交按钮的符合标准的正确语义方式是什么?我不需要按钮中的图像或背景,只需要简单的文本,也许是带弯 Angular 的渐变背景(使用 CSS3)。这些是我所知道的方法: click her
我的 App.js 包含以下代码: var app = angular.module('githubApp', []); 我有 githubAppController 和以下代码: app.contr
jQuery 允许通过以下任一方式以编程方式触发表单提交: $('.js-form-class-hook').submit(); $('.js-form-class-hook').trigger('s
我有一个表单,其中一个按钮使用 onClick 事件提交。 " 此外,我有一个输入,当输入处于焦点状态时按下回车键时会触发 couponButton。 虽然它们都触发相同的事件 (.submit()
目前只是尝试实现这些按钮:http://web.archive.org/web/20110721191046/http://particletree.com/features/rediscoverin
我正在提交我的第一个构建以供 TestFlight beta 审查,在创建一个组后,添加 4 名团队成员添加我的构建,取消单击该框以表示我需要登录信息来测试该应用程序,我单击“提交”审查。” iTun
我有一个带有 onsubmit 属性的表单。我需要绑定(bind)一个新的提交事件,并且我需要在任何现有提交函数之前执行此事件。 下面的代码演示了这个问题。 Test
我是一名优秀的程序员,十分优秀!