Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery:如何使用Each方法检查所有表单输入元素中的值_Javascript_Jquery - Fatal编程技术网

Javascript JQuery:如何使用Each方法检查所有表单输入元素中的值

Javascript JQuery:如何使用Each方法检查所有表单输入元素中的值,javascript,jquery,Javascript,Jquery,JQuery的新增功能: 好吧,我很困惑,已经尝试了所有的变体,包括我在书中读到的内容和jQueryAPI 我有以下样本标记: <form id="myForm" name="myForm" action="" method="post"> <label>*Enter Input A: <br /> <input type="text" id="inputA" name="inputA" /> <

JQuery的新增功能:

好吧,我很困惑,已经尝试了所有的变体,包括我在书中读到的内容和jQueryAPI

我有以下样本标记

<form id="myForm" name="myForm" action="" method="post">
    <label>*Enter Input A:
        <br />
        <input type="text" id="inputA" name="inputA" />
    </label>
    <label>*Enter Input B:
        <br />
        <input type="text" id="inputB" name="inputB" />
    </label>
    <label>*Enter Input C:
        <br />
        <input type="text" id="inputC" name="inputC" />
    </label>
    <label>*Enter Input D:
        <br />
        <input type="text" id="inputD" name="inputD" />
    </label>
</form>
<p class="myText">Sample Text</p>
这里有一把小提琴:


感谢您的解释

一旦满足所需条件,您就不会脱离循环。相反,您将遍历所有文本框,最终,如果最后一个文本框没有任何值,则无论前面哪个文本框有值,它都将设置为红色,因为这是迭代中的最后一个文本框。相反,您可以使用
返回false打破
.each()
循环

 $('#myForm :input[type="text"]').blur(function () {
     $('#myForm input[type="text"]').each(function () {
         if (this.value == '') {
             $(".myText").css("color", "#F00")
         } else {
             $(".myText").css("color", "#9A9A9A");
                 return false;
         }
     });
 });
因此,您可以将其简化为:

 $('#myForm :input[type="text"]').blur(function () {
     $('.myText').css('color', function () { //use the css function callback
         return $('#myForm input[type="text"]').filter(function () { //Use filter to return the textboxes which has value
             return this.value != ''
         }).length == 0 ? "#F00" : "#9A9A9A"; // set the color accordingly

     });
 });

一旦满足所需条件,您就不会脱离循环。相反,您将遍历所有文本框,最终,如果最后一个文本框没有任何值,则无论前面哪个文本框有值,它都将设置为红色,因为这是迭代中的最后一个文本框。相反,您可以使用
返回false打破
.each()
循环

 $('#myForm :input[type="text"]').blur(function () {
     $('#myForm input[type="text"]').each(function () {
         if (this.value == '') {
             $(".myText").css("color", "#F00")
         } else {
             $(".myText").css("color", "#9A9A9A");
                 return false;
         }
     });
 });
因此,您可以将其简化为:

 $('#myForm :input[type="text"]').blur(function () {
     $('.myText').css('color', function () { //use the css function callback
         return $('#myForm input[type="text"]').filter(function () { //Use filter to return the textboxes which has value
             return this.value != ''
         }).length == 0 ? "#F00" : "#9A9A9A"; // set the color accordingly

     });
 });

我更新了您的代码,请检查fiddle

当你跳出循环时,你应该改变段落的颜色:

   if(changed)$(".myText").css("color", "#9A9A9A");else
          $(".myText").css("color", "#F00");

我更新了你的代码,请检查小提琴

当你跳出循环时,你应该改变段落的颜色:

   if(changed)$(".myText").css("color", "#9A9A9A");else
          $(".myText").css("color", "#F00");

顺便说一句,不需要在
.each(函数(i,元素)
代码中使用参数,一个简单的
。each(函数(){
)也会起作用。类似地,只需使用
$(这)
在if测试中,它包含了PSL的解决方案。@事实上,乱说
这个值就足够了。。作为旁白,不需要
中的参数。每个(函数(i,元素)
代码,一个简单的
。每个(函数(){
都起作用。同样,只需使用
$(这个)
在if测试中,它包含了PSL的解决方案。@事实上,胡言乱语
这个值就足够了。
谢谢我的朋友。我一直在努力解决这个问题:我一直在阅读JQuery Missing手册,从来没有读过任何关于你刚才向我展示的关于打破循环或其他方法的内容。我想我会及时的ome better:再次感谢!感谢我的朋友。我一直在努力解决这个问题:我正在阅读JQuery Missing手册,但从未读过任何关于您刚才向我展示的打破循环或其他方法的内容。我想我会变得更好的:再次感谢!