输入更改的Jquery事件与特定类

时间:2015-06-16 01:49:23

标签: jquery forms css3 input input-field

当需要更改具有特定CSS类的输入文本字段时,我需要触发一个函数。

    <form class="form-horizontal" role="form" id="infoClient">
                                <div class="form-group">
                                    <label for="Input" class="col-md-2 col-sm-2 control-label greenIfNotEmpty">Prénom</label>
                                    <div class="col-md-4 col-sm-4">
                                        <div class="input-group">
                                            <input type="text" class="form-control" id="Input" placeholder="">
                                            <span class="input-group-addon">
                                                <i class="glyphicon  glyphicon-pencil"></i>
                                            </span>
                                        </div>
                                    </div>
                                    <label for="disabledTextInput" class="col-md-2 col-sm-2 control-label greenIfNotEmpty">Nom famille</label>
                                    <div class="col-md-4 col-sm-4">
                                        <div class="input-group">
                                            <input type="text" id="disabledTextInput2" class="form-control" placeholder="">
                                            <span class="input-group-addon">
                                                <i class="glyphicon  glyphicon-pencil"></i>
                                            </span>
                                        </div>
                                    </div>
                                </div>



    $('input[type='text']').change(function() { ... });

这适用于每个输入字段

$('input[type='text'] .greenIfNotEmpty').change(function() { ... });

无效。

有人可以帮我吗?

3 个答案:

答案 0 :(得分:2)

FIDDLE

中尝试这样的演示

<强> JS

$('input:text.red').on('keyup', function() {
    alert('hello red')
});

<强> HTML

<input type='text' class='red'/><br/>
<input type='text' class='yellow'/>

<强> CSS

.red{
    background-color:red
}

.yellow {
    background-color:yellow
}

答案 1 :(得分:0)

尝试删除输入和类名之间的空格,如下所示:

$('input[type='text'].cssClass').change(function() { ... });

答案 2 :(得分:0)

我觉得愚蠢,我把类附加到Label而不是Input。非常感谢大家。