Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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 如何在js文件中动态获取字段id?_Javascript_Jquery - Fatal编程技术网

Javascript 如何在js文件中动态获取字段id?

Javascript 如何在js文件中动态获取字段id?,javascript,jquery,Javascript,Jquery,鉴于此,我的联系方式如下: <?php echo $this->Form->create(); ?> <ul> <li> <?php echo $this->Form>input('Name',array('id'=>'name','class'=>'default'));?>

鉴于此,我的联系方式如下:

<?php echo $this->Form->create(); ?>
               <ul>
                  <li>                    
<?php echo $this->Form>input('Name',array('id'=>'name','class'=>'default'));?>
                  </li>
                  <li>                    
<?php echo $this->Form->input('Address',array('id'=>'address','class'=>'default'));?>
                  </li>
                  <li>

<?php echo $this->Form->input('Email',array('id'=>'email','class'=>'default'));?>
                  </li>
                  <li>

<?php echo $this->Form->input('Phone',array('id'=>'phone','class'=>'default'));?>
                  </li>
                  <li>

<?php echo $this->Form->textarea('Enquiry',array('id'=>'enquiry','class'=>'default'));?>
                  </li>

                </ul>
             <?php echo $this->Form->end(__('Submit')); ?>

在js文件中,我有:

$(document).ready(function(){
    $('.default').blur(function(){

        $.post( 
        '/cakephp/Packages/validate_form',
        {field:$(this).attr('id'),value:$(this).val()
        },
            handleNameValidation
    );
});

function handleNameValidation(error){


    if(error.length>0){

        if($('#name-notEmpty').length==0){
            $('#name').after('<div id="name-notEmpty" class="error-message">'+ error + "</div>");
        }
    }
    else{
        $('.error-message').remove();
    }
}
});
$(文档).ready(函数(){
$('.default').blur(函数(){
$邮政(
“/cakephp/Packages/validate_form”,
{字段:$(this.attr('id'),值:$(this.val())
},
手工验证
);
});
函数handleNameValidation(错误){
如果(错误长度>0){
如果($('#name notEmpty')。长度==0){
$('#name')。在(''+error+'')之后;
}
}
否则{
$('.error message').remove();
}
}
});
在这里,我动态地获取了错误消息,但无法在中动态地获取字段id:

$('#name').after('<div id="name-notEmpty" class="error-message">'+ error + "</div>");
$('#name')。在(''+error+'')之后;

字段。在这里,我将字段id定义为
#name
为静态字段,但需要动态字段。如需任何帮助,

我只是修改了您的代码,但未对其进行测试。试一下

$(document).ready(function(){
  $('.default').blur(function(){
     var fieldID=$(this).attr('id');
     $.post( 
      '/cakephp/Packages/validate_form',
      {field:fieldID,value:$(this).val()
      },
        handleNameValidation(error,fieldID);
    );
  });

  function handleNameValidation(error,fieldID){
     if(error.length>0){

       if($('#name-notEmpty').length==0){
         $('#'+fieldID).after('<div id="name-notEmpty" class="error-message">'+ error + "   </div>");
       }
    }
    else{
      $('.error-message').remove();
    }
 }
});
$(文档).ready(函数(){
$('.default').blur(函数(){
var fieldID=$(this.attr('id');
$邮政(
“/cakephp/Packages/validate_form”,
{field:fieldID,值:$(this.val()
},
handleNameValidation(错误,字段ID);
);
});
函数handleNameValidation(错误,字段ID){
如果(错误长度>0){
如果($('#name notEmpty')。长度==0){
$('#'+fieldID.)。在(''+error+'')之后;
}
}
否则{
$('.error message').remove();
}
}
});

我已将此问题解决为:

$(document).ready(function(){

    $('.default').blur(function(){
        var self = this;
        $.post( 
                '/cakephp/Packages/validate_form',
                {field:$(this).attr('id'),value:$(this).val()
                },
                function(error){

                    handleNameValidation(error,self)

                }
            );
            function handleNameValidation(error,el){
                 var $parent = $(el).parent();
                    if(error.length>0){
                            if($('span.error-message',$parent).length==0){
                            $parent.append('<span class="error-message">'+ error + '</span>');
                            }
                            else{
                                 $('span.error-message',$parent).replaceWith('<span class="error-message">'+ error + '</span>');
                            }
                    }
                    else{
                         $parent.removeClass('error-message');
                         $('span.error-message',$parent).fadeOut();
                    }
            }
    });
$(文档).ready(函数(){
$('.default').blur(函数(){
var self=这个;
$邮政(
“/cakephp/Packages/validate_form”,
{字段:$(this.attr('id'),值:$(this.val())
},
函数(错误){
handleNameValidation(错误,自我)
}
);
函数handleNameValidation(错误,el){
var$parent=$(el.parent();
如果(错误长度>0){
if($('span.error message',$parent).length==0){
$parent.append(“”+错误+“”);
}
否则{
$('span.error message',$parent).replace为(''+error+'');
}
}
否则{
$parent.removeClass('error-message');
$('span.error message',$parent.fadeOut();
}
}
});

我之前尝试过这个方法,但不起作用。无法在函数中获取fieldId值