35
如何检查表格“文本框,复选框,textarea,select,file”中的所有元素是否都不为空?如何检查表单元素是否为空?
如何检查表格“文本框,复选框,textarea,select,file”中的所有元素是否都不为空?如何检查表单元素是否为空?
你可以看到如果有任何空这样的:
$(":input").each(function() {
if($(this).val() === "")
alert("Empty Fields!!");
});
You can read on the :input selector here
一个更具体的答案,如果你只想要那些类型,改变这样的选择:
$(":text, :file, :checkbox, select, textarea").each(function() {
if($(this).val() === "")
alert("Empty Fields!!");
});
+1对于'$(“:text,:file,:checkbox,select,textarea”)'每个部分。 – ant 2010-03-18 13:28:41
.val()不起作用:复选框 – trrrrrrm 2010-03-18 14:59:44
@ From.ME.to.YOU - You想要求是否所有复选框都被检查?确保你首先要做到这一点,但通常你不这样做。如果它是“同意条款”,这是有道理的......如果不是那么为什么有复选框而不是“真”?如果你真的需要他们检查改变如果这个:if(($(this).is(“:checkbox”)&&!$(this).is(“:checked”))|| $(this) .val()===“”)' – 2010-03-18 15:30:29