简体   繁体   English

使用javascript检查多个输入值并在1个复选框上标记多个输入框

[英]check multiple input values with javascript and marking multiple input boxes on 1 check

I expected the following function to apply a red border to BOTH the input field with id 'name', AS the input field with the id 'email'. 我期望以下功能将红色边框同时应用于ID为“ name”的输入字段和AS ID为“ email”的输入字段。 (when the user leaves all input-fields empty) (当用户将所有输入字段留空时)

However, the script only applies the red border to the input-field with id 'name', and leaves the field with id 'email' untouched. 但是,该脚本仅将红色边框应用于ID为“ name”的输入字段,而ID为“ email”的字段保持不变。

            function checkform() {
                if ( name() && mail() ) {
                    return true;
                } else {
                    return false;
                }
            }

            function name() {
                if (document.getElementById("name").value == "") {
                    $("#name").css("border", "1px solid red");
                    return false;
                } else { 
                    return true; 
                }
            }

            function mail() {
                if (document.getElementById("email").value == "") {
                    $("#email").css("border", "1px solid red");
                    return false;
                } else { 
                    return true; 
                }
           }

It seems like the return false in the sub-function 'name()' somehow 'kills' the entire code, and stops 'mail()' from every being run. 子函数“ name()”中的return false似乎以某种方式“杀死”了整个代码,并阻止了“ mail()”的运行。

Update : If I switch the function-names in the 2nd line only the input-field with id 'email' gets a red border (if user leaves all input-fields empty). 更新 :如果我在第二行切换功能名称,则只有ID为'email'的输入字段会显示红色边框(如果用户将所有输入字段留空)。 Then the field with id 'name' remains untouched. 然后,ID为“ name”的字段保持不变。

(Sorry for using javascript and jquery at the same time) (很抱歉同时使用javascript和jquery)

The return false statement in the name function is short-circuiting the statement inside the if condition. 名称函数中的return false语句使if条件内的语句短路。 While evaluating if( name() && mail() ), name() returns false and since AND with false always gives false, the statement is short-circuited and the mail() function is never called. 在评估if(name()&& mail())时,name()返回false,并且由于AND与false始终为false,因此该语句被短路,并且永远不会调用mail()函数。

If statement runs the conditions one by one until it is sure that they don't fulfill the condition. 如果if语句逐个运行条件,直到确定它们不满足条件。 In your case if name() is false it will never run the conditions after it. 在您的情况下,如果name()为false,它将永远不会在其后运行条件。 In your case you can try using something like: 您可以尝试使用以下方法:

function checkform() {
   var isValid = name();
   isValid = mail() && isValid;
   return isValid;
}

However I recommend you to write a generic function that will check if some input given as parameter is empty. 但是,我建议您编写一个通用函数,该函数将检查作为参数给出的某些输入是否为空。

Also there is quite nice jQuery plugin that might help you with your task: http://jqueryvalidation.org/ 另外还有一个非常不错的jQuery插件,可以帮助您完成任务: http : //jqueryvalidation.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM