Javascript 如何使用 jQuery 选择空输入 (value="")
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10651349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select empty inputs (value="") using jQuery
提问by Richard Harris
How can I check for empty values of (required
) input fields within a section, and then add a class to them on an event, using jQuery? So far, I have tried:
如何required
使用 jQuery检查某个部分中 ( ) 输入字段的空值,然后在事件中向它们添加一个类?到目前为止,我已经尝试过:
jQuery("#sender_container input.required").val("").addClass("error");
But that seems to SETthe value, rather than checking it. Any ideas?
但这似乎是设置值,而不是检查它。有任何想法吗?
回答by gdoron is supporting Monica
jQuery("#sender_container input.required").filter(function() {
return !this.value;
}).addClass("error");?
Why you have to use filter
and not [value=""]
you can see in this DEMO
为什么你必须使用filter
而不是[value=""]
你可以在这个 DEMO 中看到
The reason is: attribute selectors check the initial state of the element, not the current state. (note that you can change the "initial" state with the attr
function, but it's bad practice, you should always use prop
)
原因是:属性选择器检查元素的初始状态,而不是当前状态。(请注意,您可以使用该attr
函数更改“初始”状态,但这是不好的做法,您应该始终使用prop
)
So if you change the input value, the current value won't effect the attribute selector. not wise... :)
因此,如果您更改输入值,当前值不会影响属性选择器。不明智...:)
Notes:
笔记:
.val()
returnsthe value of the form element, and breaks the jQuery chain,$('selector').val().addClass('foo')
Error, the return value is a string\ number.val(valueToSet)
setsthe value of the form element and doesn't break the jQuery chain.$('selector').val("some value").addClass('foo')
- Valid, the returned value is a jQuery
.val()
返回表单元素的值,打断jQuery链,$('selector').val().addClass('foo')
Error,返回值是一个字符串\数字.val(valueToSet)
设置表单元素的值并且不会破坏 jQuery 链。$('selector').val("some value").addClass('foo')
-有效,返回值是一个 jQuery
回答by Parv Sharma
$('#sender_container input.required[value=""]').addClass('error')
回答by thecodeparadox
回答by Nils
$field = $("#sender_container input.required");
if( ! $field.val())
{
$field.addClass("error");
}
this simple way may work.
这种简单的方法可能会奏效。
回答by schmijos
If you only need to select based on the initial attribute value of the input then the following will do:
如果您只需要根据输入的初始属性值进行选择,则执行以下操作:
var elements = $('#sender_container input.required[value=""]')
But be aware that this won't work if the value
attribute isn't present. It also won't work for the current input value if it has been changed by user or script.
但请注意,如果该value
属性不存在,这将不起作用。如果当前输入值已被用户或脚本更改,它也不适用于当前输入值。
If you'd like to get the current input value you can use jquery's filter
function:
如果您想获取当前输入值,可以使用jquery的filter
函数:
var elements = $('#sender_container input.required').filter(function() {
return this.value === '';
// alternatively for "no value":
// return !this.value;
})
After you've selected the jqueryelements you can add your class:
选择jquery元素后,您可以添加类:
elements.addClass('error');
回答by rmil
to get all fields inspected this might help.
要检查所有字段,这可能会有所帮助。
$('#sender_container [required]').each(function(index)
{
if (!($(this).val())) $(this).addClass('error');
}
});
回答by Masood
For those input.requiredis not working write as below:
对于那些input.required不起作用写如下:
jQuery('#sender_container input[required]').val("").addClass("error");