admin管理员组文章数量:1332377
I have a div which contain lots of input[type=text]
fields. I want to put a check that user must have fill all the fields otherwise show error message. For this i tried :
if ($('div').children("input[type=text]").attr("value") == '')
flag = false;
else
flag = true;
But this will not check all the text field, it only checks the first-child . How can i check all the text field ?
I have a div which contain lots of input[type=text]
fields. I want to put a check that user must have fill all the fields otherwise show error message. For this i tried :
if ($('div').children("input[type=text]").attr("value") == '')
flag = false;
else
flag = true;
But this will not check all the text field, it only checks the first-child . How can i check all the text field ?
Share asked Jul 5, 2012 at 9:49 Tom RiderTom Rider 2,8157 gold badges44 silver badges66 bronze badges3 Answers
Reset to default 3Try to use find()
instead
if ($('div').find("input[type=text]").val() == '')
To check over all input items you need to iterate over them using each()
.
Correct code would be:
$(function() {
$('input:submit').click(function(){
var flag = true;
$('div').find("input[type=text]").each(function(){
if($(this).val() === '') flag = false
});
alert(flag);
});
});
See demo on jsFiddle.
This code return true
if all input[type=text]
fields inside div
are filled and false
if at least one is empty.
The standard way
if(!$('div').find('#idOfTextField').val()){
//empty
}
if($("div").find('input:text[value=""]').length > 0)
{
alert("error");
return false;
}
本文标签: javascriptcheck whether text field is empty or not jqueryStack Overflow
版权声明:本文标题:javascript - check whether text field is empty or not jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742296509a2448837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论