Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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
突出显示多输入ID';关于javascript验证的讨论_Javascript_Jquery_Forms_Validation - Fatal编程技术网

突出显示多输入ID';关于javascript验证的讨论

突出显示多输入ID';关于javascript验证的讨论,javascript,jquery,forms,validation,Javascript,Jquery,Forms,Validation,使用javascript验证和一些jquery在DOM中显示变量错误消息,并高亮显示表单标签和字段 对于这个特定的错误消息,我需要突出显示两个字段(email1和email2)。 不确定如何将其添加到我当前的警报设置中: customAlert ("email2",bnadd_msg_022); 简单的问题是,如何将email1添加到组合中 编辑: 以下是我的jquery函数: function customAlert(inputID,msg){ $("li").removeCl

使用javascript验证和一些jquery在DOM中显示变量错误消息,并高亮显示表单标签和字段

对于这个特定的错误消息,我需要突出显示两个字段(email1和email2)。 不确定如何将其添加到我当前的警报设置中:

customAlert ("email2",bnadd_msg_022);
简单的问题是,如何将email1添加到组合中

编辑:

以下是我的jquery函数:

function customAlert(inputID,msg){

      $("li").removeClass("alertRed");
      $("input").removeClass("CO_form_alert");  
      $("select").removeClass("CO_form_alert");   
      var div = $(".errorPopup");
      div.css({"display":"block"});
      $("#"+inputID).addClass("CO_form_alert").parent().addClass("alertRed");
      if (div.length == 0) {
        div = $("<div class='errorPopup' onclick='$(this).hide();'></div>");
        $("body").prepend(div);
      } 
      div.html(msg);
      $("#"+inputID).focus(function(){
        $(this).unbind('focus'); // remove this handler
        $('.errorPopup').hide(); // hide error popup
   });
函数customAlert(inputID,msg){
$(“li”).removeClass(“alertRed”);
$(“输入”).removeClass(“CO表格警报”);
$(“选择”).removeClass(“CO表格警报”);
var div=$(“.errorPopup”);
css({“display”:“block});
$(“#”+inputID).addClass(“CO_表单_警报”).parent().addClass(“警报”);
如果(div.length==0){
div=$(“”);
$(“正文”)。预付款(部门);
} 
div.html(msg);
$(“#”+inputID).focus(函数(){
$(this).unbind('focus');//删除此处理程序
$('.errorPopup').hide();//隐藏错误弹出窗口
});

因此,在本例和另一例中,我需要同时突出显示两个字段,而不查看代码,我猜您可以更改customAlert函数,以获取可选的第三个参数


我的方法是将第三个参数设置为一个数组,该数组可以接受n个字段id。在customAlert函数中,我将检查该参数是否已传递,如果已传递,则循环遍历该数组并突出显示其中包含的任何id。

我将使用Javascript中的参数“数组”,并简单地定义customAlert a这是一个没有参数的函数

function customAlert(){
    var args = arguments;
    if(args.length > 1) {
        // check that custom alert was called with at least two arguments
        var msg = args[0];
        $("li").removeClass("alertRed");
        $("input").removeClass("CO_form_alert");  
        $("select").removeClass("CO_form_alert");   
        var div = $(".errorPopup");
        div.css({"display":"block"});
        if (div.length == 0) {
            div = $("<div class='errorPopup' onclick='$(this).hide();'></div>");
            $("body").prepend(div);
        } 
        div.html(msg);
        for(var i = 1; i < args.length; i++) {
            var inputID = args[i];
           $("#"+inputID).addClass("CO_form_alert").parent().addClass("alertRed");
            $("#"+inputID).focus(function(){
                $(this).unbind('focus'); // remove this handler
                $('.errorPopup').hide(); // hide error popup
            });
        }
     }
}

这将取决于“customAlert()”是什么函数看起来像。如果你能发布简单的HTML和JS,你会变得更好answers@Pointy@Bobby Borszich-请参见上面的编辑。谢谢。假设单个msg参数足够,这可能会起作用。不幸的是,使用上述代码,标签和表单字段的突出显示不再出现-具体为addClass-CO_form_alert及alertRed@Jason我错过了代码中的那一行-我将更新答案以将其包含在for循环中。非常漂亮。工作非常出色。我感谢您的帮助。我正在更彻底地学习jquery和javascript(我知道的大部分内容都与前端开发、视觉效果等有关),因此我很欣赏这些知识
customAlert(bnadd_msg_022,"email2"); // note the order of the arguments has changed
customAlert(bnadd_msg_022,"email1","email2");