Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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
Php removeClass(“has error”)在字段更改时启动,javascript 我的Javascript 我的表格_Php_Jquery_Twitter Bootstrap - Fatal编程技术网

Php removeClass(“has error”)在字段更改时启动,javascript 我的Javascript 我的表格

Php removeClass(“has error”)在字段更改时启动,javascript 我的Javascript 我的表格,php,jquery,twitter-bootstrap,Php,Jquery,Twitter Bootstrap,现在,当我修改代码时,我添加了: $('.input-group').each(function(){ $(this).find('.form-control').change(function(){ $(this).closest('div').removeClass('has-error').closest('span').removeClass('glyphicon-remove'); }); }); 在和相同的东西中,我想删除g

现在,当我修改代码时,我添加了:

$('.input-group').each(function(){
    $(this).find('.form-control').change(function(){
        $(this).closest('div').removeClass('has-error').closest('span').removeClass('glyphicon-remove');            
    });
});
在和相同的东西中,我想删除glyphicon删除类,就像.form控件字段更改后的has error类一样

$('.input-group').each(function(){
    $(this).find('.form-control').change(function(){
        $(this).closest('div').removeClass('has-error').closest('span').removeClass('glyphicon-remove');            
    });
});

我播放了代码,但glyphicon remove类仍然存在。

不确定您到底想做什么:只需从已更改的
的父级中删除.has error类即可

$('.input-group').each(function(){
    $(this).find('.form-control').change(function(){
        $(this).closest('div').removeClass('has-error').closest('span').removeClass('glyphicon-remove');            
    });
});
$('.input group.form control').change(function(){
$(this.parent().removeClass('has-error');

});

如果元素具有类,则可以简单地附加事件

$('.input-group').each(function(){
    $(this).find('.form-control').change(function(){
        $(this).closest('div').removeClass('has-error').closest('span').removeClass('glyphicon-remove');            
    });
});

$('.input-group').each(function(){
    $(this).find('.form-control').change(function(){
        $(this).closest('div').removeClass('has-error').closest('span').removeClass('glyphicon-remove');            
    });
});

您需要做一些更改:

$('.input-group').each(function(){
    $(this).find('.form-control').change(function(){
        $(this).closest('div').removeClass('has-error').closest('span').removeClass('glyphicon-remove');            
    });
});
$('.form-control').find('input[type="text"]').on('input', function() {
     $(this).closest('.input-group').removeClass('has-error');        
});
  • 在输入类型文本上使用
    keydown
    ,而不是
    change
    事件,因为只有当您从元素上松开焦点时,才会发生更改事件
  • 使用
    $进入上下文(this)
    使用
    .closest()
    到达所需的父级,然后从中删除该类
  • $(':input')。在('input',函数(e)上{
    console.log('type',e.type);
    });
    
    
    
    使用
    $(文档)。在('input')…
    上监视对动态和静态元素的所有更改:

    $('.input-group').each(function(){
        $(this).find('.form-control').change(function(){
            $(this).closest('div').removeClass('has-error').closest('span').removeClass('glyphicon-remove');            
        });
    });
    
    $(document).on('input', '.form-control', function(){
      $(this).closest('.input-group').removeClass('has-error');
    });
    

    因此,您要删除
    .has error
    类?
    $('.has error')。removeClass('.has error');
    keydown
    不监视
    右键单击>粘贴
    @rybo111,即
    .on()
    方法对于在单个选择器上绑定多个事件非常有用。请参阅更新。
    键下粘贴输入将在粘贴时运行该函数两次。这取决于元素的创建方式,OP没有提到这一点,根据您最后的注释
    输入
    将仅作为选项选择。对于动态创建的元素,e可以将vent委托给最近的静态父级,如元素的包装器,无论是div还是文档/正文本身。即使OP现在不使用动态输入(我们不知道),他以后也可能会使用。如果您可以支持,也可以这样做。最近的div可能不是包含
    has error
    的div。因此-
    最近的div('div.has-error')
    最近的('has error')
    谢谢你…我几乎曾经认为我可以使用附近最近的()东西,但没有这样向前迈进…我修改了你的代码,删除了.has error类,仍然可以正常工作…$(function(){$('div.input-group')。每个(function(){$(this)。查找('input').change(function(){$(this).closest('div').removeClass('has-error');});@EmersonMatiasPaguia我放置
    has error
    类的原因是只将处理程序添加到错误元素中-它的清理程序,并避免不必要的处理程序。但两者都可以工作。