使用 jQuery 检查每个必需的输入是否为空值
我正在检查表单中的每个必填字段。我尝试过这个,但它只适用于第一个输入。我怎样才能让它适用于每个输入?
if($('[required]').val() != ''){
$('.button1').fadeIn(0);
$('.button2').fadeOut(0);
}else{
$('.button1').fadeOut(0);
$('.button2').fadeIn(0);
}
编辑:为了了解更多上下文,下面是我在下面的另一个函数,每当它们和 HTML 代码发生更改时都会应用它。
$('[required]').change(function() {
if($('[required]').val() != ''){
$('.button1').fadeIn(0);
$('.button2').fadeOut(0);
}else{
$('.button1').fadeOut(0);
$('.button2').fadeIn(0);
}
});
HTML
我有一堆像这样的输入:
<input type="text" id="title" name="title" value="" required>
<input type="text" id="size" name="size" value="" required>
<input type="text" id="length" name="length" value="" required>
按钮:
<a class="button1" style="display:none" >button 1</a>
<a class="button2">button 2</a>
所以我想在页面加载时以及每当它们发生变化时检查空字段。
顺便说一句,我没有围绕这些表单标签,因为它是针对移动设备的,并且认为它并不真正有必要(如果这有什么区别的话)。
再次感谢!
I am checking for each required fields in a form. I tried this but it only works for the first input. How can I make it work for each input?
if($('[required]').val() != ''){
$('.button1').fadeIn(0);
$('.button2').fadeOut(0);
}else{
$('.button1').fadeOut(0);
$('.button2').fadeIn(0);
}
EDIT: for more context, here's another function I had below this to apply whenever they change and HTML code.
$('[required]').change(function() {
if($('[required]').val() != ''){
$('.button1').fadeIn(0);
$('.button2').fadeOut(0);
}else{
$('.button1').fadeOut(0);
$('.button2').fadeIn(0);
}
});
HTML
I have bunch of inputs like these:
<input type="text" id="title" name="title" value="" required>
<input type="text" id="size" name="size" value="" required>
<input type="text" id="length" name="length" value="" required>
buttons:
<a class="button1" style="display:none" >button 1</a>
<a class="button2">button 2</a>
So I want to check for empty fields when it the page loads and whenever they change.
BTW, I don't have a form tag around these because it's for mobile and didn't think it's really necessary, if that makes any difference.
Thanks again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这?
其中
yourForm
是对 FORM 元素的引用(您也可以在此处使用选择器)。这是上下文 - 您只想搜索表单内的字段。This?
where
yourForm
is a reference to the FORM element (you can also use a selector here). This is the context - you only want to search for fields inside the form.“change”事件适用于 SELECT 框和 RADIO 按钮元素,但不适用于 INPUT 元素。您必须使用“焦点/模糊”事件来处理输入字段,在您的情况下,“模糊”事件非常适合。
如果您想使用 jQuery 处理许多元素,那么最好使用基于 CLASS 的操作,在您的情况下,只需为所有输入字段添加“必需”相同的类,并使用该类对这些字段进行操作。
我已经按照上面指定的方式调整了您的代码块,检查一次,然后让我知道更新的代码是否也不适合您,更新的代码块是:
HTML 代码
JS 代码< /b>
您还可以直接在此处检查工作代码。
The "change" event works for the SELECT box and RADIO button elements but not for the INPUT elements. You've to use the "focus/blur" events for handling the input fields, in your case the "blur" event suits perfectly.
If you want to process many elements using jQuery then it's better to go with the CLASS based operations, in your case just add a same class "required" for all the input fields and make the operations on those fields using that class.
I've adjusted your code blocks as I specified above, check them once and then let me know if the updated code also not working for you, updates code blocks are:
HTML Code
JS Code
You can also check the working code directly here.
使用
each()
函数 (doc):Use the
each()
function (doc):您需要获取每个元素的值,如果所有元素都不为空,则执行第一件事,如果其中任何元素为空,则执行第二件事,对吗?
[未经测试]
You need to grab the value of each element, and if ALL of them are not empty, do the first thing, and if ANY of them are empty, do the second thing, correct?
[untested]
这是我的代码
This My Code
简单,为了增加测量效果,添加一个红色边框,
首先制作一个样式
,然后编写一些简单的 Javascript
Simple, and for added measure add a red border
First make a style
Then a simple bit of Javascript