Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.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 如何使用带或条件的多类选择器_Javascript_Jquery - Fatal编程技术网

Javascript 如何使用带或条件的多类选择器

Javascript 如何使用带或条件的多类选择器,javascript,jquery,Javascript,Jquery,我有4门课,分别是Agefrom,Ageto,coverage,premium。这些类用于文本框。每当anytextbox的文本发生变化时,我都想做些什么。我编写了这个jQuery,但它不起作用: <table> <tr> <td><input class="Agefrom" type="text"/></td> <td><input class="Ageto" type="te

我有4门课,分别是
Agefrom
Ageto
coverage
premium
。这些类用于文本框。每当anytextbox的文本发生变化时,我都想做些什么。我编写了这个jQuery,但它不起作用:

<table>
    <tr>
        <td><input class="Agefrom" type="text"/></td>
        <td><input class="Ageto" type="text"/></td>
        <td><input class="coverage" type="text"/></td>
        <td><input class="premium" type="text"/></td>
    </tr>
</table>

首先,您使用的是从jQuery 2.0+中删除的
live
。改为在上使用
。其次,类选择器区分大小写。试试这个:

$('[class^=Age], .coverage, .premium').on('change', function () {
    alert('do something');
});

还要注意,文本字段上的
change
事件在元素失去焦点之前不会触发。如果需要在值发生更改时立即触发事件,请使用
keyup

更好的解决方案是在所有必需的元素上使用一个公共类,并仅选择该类。

在现有类之外的输入上添加一些其他公共类(例如,
watchChange
),并在该类上进行匹配,即:

<td><input class="watchChange Agefrom" type="text"/></td>
<td><input class="watchChange Ageto" type="text"/></td>
<td><input class="watchChange coverage" type="text"/></td>
<td><input class="watchChange premium" type="text"/></td>

$('.watchChange').on('change', document) ...

$('watchChange')。在('change',document)。。。

您应该在
上使用
,因为
live
已被弃用,并且选择器区分大小写:

$(document).ready(function(){
    $('[class^=Age],.coverage,.premium').on('change', function () {
        alert('do something');
    });
});

您可以为此使用
分隔符

live是不推荐使用的。您可以在
上使用
,而不是此选项。但是你的元素不是动态的。所以你可以这样用

$('.Agefrom,.Coverage,.Premium').change(function () {
        alert('do something');
    });
如果元素是动态生成的,请使用
on
dellegate进行绑定事件

$(document).on("change",'.Agefrom,.Coverage,.Premium',function () {
            alert('do something');
        });

试试这个,会帮你很多

$(document).ready(function(){
    $('[class^=Age],.coverage,.premium').on('change', function () {
        alert('do something');
    });
});

小提琴链接

最好在行中添加一个类并在其中查找所有输入?差不多

<table>
    <tr class="rowclass">
        <td><input class="Agefrom" type="text"/></td>
        <td><input class="Ageto" type="text"/></td>
        <td><input class="coverage" type="text"/></td>
        <td><input class="premium" type="text"/></td>
    </tr>
</table>

可能最好找到输入?
<table>
    <tr class="rowclass">
        <td><input class="Agefrom" type="text"/></td>
        <td><input class="Ageto" type="text"/></td>
        <td><input class="coverage" type="text"/></td>
        <td><input class="premium" type="text"/></td>
    </tr>
</table>
$(document).ready(function(){
    $('.rowclass input').on('change', function () {
        alert('do something');
    });
});