Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.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 如何在jQuery方法上使用多选择器?_Javascript_Jquery - Fatal编程技术网

Javascript 如何在jQuery方法上使用多选择器?

Javascript 如何在jQuery方法上使用多选择器?,javascript,jquery,Javascript,Jquery,我已经创建了两个选择项,并使用onjQuery方法选择了检查项 我有两个选择器id(#idchief,#teller),我将使用$(document).on方法检查项目是否被选中,但我不想使用document.on()两次。我想检查选择上是否已选择,以及此选择的过程 $(document).on('change','#idchief', function (event) { console.log($("#idchief")); var id = parseInt($("#id

我已经创建了两个选择项,并使用
on
jQuery方法选择了检查项

我有两个选择器id(#idchief,#teller),我将使用
$(document).on
方法检查项目是否被选中,但我不想使用document.on()两次。我想检查选择上是否已选择,以及此选择的过程

$(document).on('change','#idchief', function (event) {

    console.log($("#idchief"));
    var id = parseInt($("#idchief").val());

    for(var keys in cheifs) {
        var vals = cheifs[keys];
        if(parseInt(vals.id) === id){
            console.log(vals.id);
        }
    }
});

$(document).on('change','#teller', function (event) {

    var vals = null;
    var sel = $("#teller").val();
    for(var key in tellers) {
        vals = tellers[key];
            console.log(sel)
        if(vals.id == sel){

            $('input[name=amount]').val(vals.balance)
        }
    }
});
我试过这个

$(document).on('change','#teller,#idchief', function (event) {

   if($teller){
       //code here
   }if(#idchief)(
     ///code here
   )
});
使用


@图萨的回答是正确的,但有一个小问题。如果单击
#teller
的子级
$(此).is(“#teller”)
将生成错误的
false

要解决此问题,请检查此是否为#出纳员或其父项之一是否为#出纳员

$(document).on('change','#teller, #idchief', function (event) {

    var that = $('this')
    if (that.is('#teller') || that.parents('#teller').size()) {
        // Code here
    }

    if (that.is('#idchief') || that.parents('#idchief').size()) {
        // Code here
    }
}
编辑

如果您使用的是jQuery 1.8版或更高版本,请使用而不是


我建议你给他们上一节课,然后测试一下

请注意,this.id、$(this).attr(“id”)和$(this).is(“#is”)都是工作:

$(函数(){
$(“#内容”).on(“更改”,“.sel”,函数(){
如果(this.id==“sel1”){
控制台日志(“sel1”);
}
如果($(this).is(“#sel2”){
控制台日志(“sel2”);
}  
else if($(this.attr(“id”)===“sel3”){
控制台日志(“sel3”);
}  
else console.log(“不是预期的”);
});
});

请选择
一个
两个
请选择
三
四
请选择
五
六

$(文档)。在('change'、'#teller、#idchief',函数(e){
尝试此操作为两者设置一个相同的类,然后检查其id并根据id@guradio这不正是他所做的吗?将项目赋予相同的类并使用
$(document).on('change','select',function(event){if(this.id==“teller”){//code here
如果使用此方法,它将尝试查找类选择器并返回id为什么不使用is()方法,并检查每个id选择器?如果介于$(this).is和$(this.attr('id')之间,哪一个更好“HengSopheak,我认为比较字符串比对象更好,你的代码也是工作的,但是我只考虑了A.ANA UMER代码,因为它可以避免一些不方便的错误发生。”HysSopHek不需要测试PalTeNo使用它,而不是因为它只有IF和For条件,你能告诉我为什么我们需要使用一些原因吗?d size()方法?将返回父级(“#teller”)选择的元素的数量。
如果为零,则表示单击的项不是
#teller
的子项,条件求值为false,否则为true。我再次否决了。我刚刚测试过,单击子项时也会触发is!!!
$(document).on('change','#teller, #idchief', function (event) {

    var that = $('this')
    if (that.is('#teller') || that.parents('#teller').size()) {
        // Code here
    }

    if (that.is('#idchief') || that.parents('#idchief').size()) {
        // Code here
    }
}
if (that.is('#teller') || that.parents('#teller').length) {
    // Code here
}