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