Jquery 如何在多选择器功能中识别特定选择器?

Jquery 如何在多选择器功能中识别特定选择器?,jquery,Jquery,我不知道该如何表达这个问题,所以如果不清楚,我道歉。我有一段代码,当3个下拉菜单中的任何一个发生变化时,它会运行AJAX查询: $('#ass-seenByName, #ass-seenByName1, #ass-seenByName2').change(function() { $.ajax({ type: "GET", url: "getMDSSpeciality.php", data: { "mds_name": $(this).val() }, a

我不知道该如何表达这个问题,所以如果不清楚,我道歉。我有一段代码,当3个下拉菜单中的任何一个发生变化时,它会运行AJAX查询:

$('#ass-seenByName, #ass-seenByName1, #ass-seenByName2').change(function() {
    $.ajax({
    type: "GET",
    url: "getMDSSpeciality.php",
    data: { "mds_name": $(this).val() },
    async: false,
    success: function(msg){
        alert("Hello " + msg);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        databaseError({
        title: "Database error",
        string: "Unable to get speciality for selected individual",
        file: window.location.pathname,
        actualError: errorThrown
        });
    } 
    }); 
});

返回的
msg
值需要转到3个字段之一,这取决于更改的特定下拉菜单。我的问题是,我不知道如何在jQuery中获得3个可能的下拉菜单中的哪一个已更改,而不为每个单独的下拉菜单复制此代码块3次。

您可以在事件处理程序中使用
this
来引用触发处理程序的元素,因此,您可以创建一个闭包变量,该变量将引用click处理程序中的元素,该元素可在成功处理程序中用于设置消息

$('#ass-seenByName, #ass-seenByName1, #ass-seenByName2').change(function () {
    var $this = $(this);
    $.ajax({
        type: "GET",
        url: "getMDSSpeciality.php",
        data: {
            "mds_name": $(this).val()
        },
        async: false,
        success: function (msg) {
            alert("Hello " + msg);
            $this.html(msg)
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            databaseError({
                title: "Database error",
                string: "Unable to get speciality for selected individual",
                file: window.location.pathname,
                actualError: errorThrown
            });
        }
    });
});

您可以在事件处理程序中使用
this
来引用触发处理程序的元素,因此您可以创建一个闭包变量,该变量将引用click处理程序中的元素,该元素可在成功处理程序中用于设置消息

$('#ass-seenByName, #ass-seenByName1, #ass-seenByName2').change(function () {
    var $this = $(this);
    $.ajax({
        type: "GET",
        url: "getMDSSpeciality.php",
        data: {
            "mds_name": $(this).val()
        },
        async: false,
        success: function (msg) {
            alert("Hello " + msg);
            $this.html(msg)
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            databaseError({
                title: "Database error",
                string: "Unable to get speciality for selected individual",
                file: window.location.pathname,
                actualError: errorThrown
            });
        }
    });
});

您可以在事件处理程序中使用
this
来引用触发处理程序的元素,因此您可以创建一个闭包变量,该变量将引用click处理程序中的元素,该元素可在成功处理程序中用于设置消息

$('#ass-seenByName, #ass-seenByName1, #ass-seenByName2').change(function () {
    var $this = $(this);
    $.ajax({
        type: "GET",
        url: "getMDSSpeciality.php",
        data: {
            "mds_name": $(this).val()
        },
        async: false,
        success: function (msg) {
            alert("Hello " + msg);
            $this.html(msg)
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            databaseError({
                title: "Database error",
                string: "Unable to get speciality for selected individual",
                file: window.location.pathname,
                actualError: errorThrown
            });
        }
    });
});

您可以在事件处理程序中使用
this
来引用触发处理程序的元素,因此您可以创建一个闭包变量,该变量将引用click处理程序中的元素,该元素可在成功处理程序中用于设置消息

$('#ass-seenByName, #ass-seenByName1, #ass-seenByName2').change(function () {
    var $this = $(this);
    $.ajax({
        type: "GET",
        url: "getMDSSpeciality.php",
        data: {
            "mds_name": $(this).val()
        },
        async: false,
        success: function (msg) {
            alert("Hello " + msg);
            $this.html(msg)
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            databaseError({
                title: "Database error",
                string: "Unable to get speciality for selected individual",
                file: window.location.pathname,
                actualError: errorThrown
            });
        }
    });
});
是的,使用“this”,如果要附加到select的选项, 试试这个

$('#ass-seenByName, #ass-seenByName1, #ass-seenByName2').change(function () {
    $.ajax({
        type: "GET",
        url: "getMDSSpeciality.php",
        data: {
            "mds_name": $(this).val()
        },
        async: false,
        success: function (msg) {
            $(this).empty();//empties the select 
            $(this).append($('<option>').text(msg).attr('value', msg));//appends to options            
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            databaseError({
                title: "Database error",
                string: "Unable to get speciality for selected individual",
                file: window.location.pathname,
                actualError: errorThrown
            });
        }
    });
});
$('#ass-seenByName,#ass-seenByName1,#ass-seenByName2')。更改(函数(){
$.ajax({
键入:“获取”,
url:“GetMDSSSpeciality.php”,
数据:{
“mds_名称”:$(this.val()
},
async:false,
成功:功能(msg){
$(this).empty();//清空选择
$(this.append($('').text(msg.attr('value',msg));//附加到选项
},
错误:函数(XMLHttpRequest、textStatus、errorshown){
数据库错误({
标题:“数据库错误”,
字符串:“无法获取所选个人的专业”,
文件:window.location.pathname,
促动器错误:错误抛出
});
}
});
});
是,请使用“this”,如果您想附加到select的选项中, 试试这个

$('#ass-seenByName, #ass-seenByName1, #ass-seenByName2').change(function () {
    $.ajax({
        type: "GET",
        url: "getMDSSpeciality.php",
        data: {
            "mds_name": $(this).val()
        },
        async: false,
        success: function (msg) {
            $(this).empty();//empties the select 
            $(this).append($('<option>').text(msg).attr('value', msg));//appends to options            
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            databaseError({
                title: "Database error",
                string: "Unable to get speciality for selected individual",
                file: window.location.pathname,
                actualError: errorThrown
            });
        }
    });
});
$('#ass-seenByName,#ass-seenByName1,#ass-seenByName2')。更改(函数(){
$.ajax({
键入:“获取”,
url:“GetMDSSSpeciality.php”,
数据:{
“mds_名称”:$(this.val()
},
async:false,
成功:功能(msg){
$(this).empty();//清空选择
$(this.append($('').text(msg.attr('value',msg));//附加到选项
},
错误:函数(XMLHttpRequest、textStatus、errorshown){
数据库错误({
标题:“数据库错误”,
字符串:“无法获取所选个人的专业”,
文件:window.location.pathname,
促动器错误:错误抛出
});
}
});
});
是,请使用“this”,如果您想附加到select的选项中, 试试这个

$('#ass-seenByName, #ass-seenByName1, #ass-seenByName2').change(function () {
    $.ajax({
        type: "GET",
        url: "getMDSSpeciality.php",
        data: {
            "mds_name": $(this).val()
        },
        async: false,
        success: function (msg) {
            $(this).empty();//empties the select 
            $(this).append($('<option>').text(msg).attr('value', msg));//appends to options            
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            databaseError({
                title: "Database error",
                string: "Unable to get speciality for selected individual",
                file: window.location.pathname,
                actualError: errorThrown
            });
        }
    });
});
$('#ass-seenByName,#ass-seenByName1,#ass-seenByName2')。更改(函数(){
$.ajax({
键入:“获取”,
url:“GetMDSSSpeciality.php”,
数据:{
“mds_名称”:$(this.val()
},
async:false,
成功:功能(msg){
$(this).empty();//清空选择
$(this.append($('').text(msg.attr('value',msg));//附加到选项
},
错误:函数(XMLHttpRequest、textStatus、errorshown){
数据库错误({
标题:“数据库错误”,
字符串:“无法获取所选个人的专业”,
文件:window.location.pathname,
促动器错误:错误抛出
});
}
});
});
是,请使用“this”,如果您想附加到select的选项中, 试试这个

$('#ass-seenByName, #ass-seenByName1, #ass-seenByName2').change(function () {
    $.ajax({
        type: "GET",
        url: "getMDSSpeciality.php",
        data: {
            "mds_name": $(this).val()
        },
        async: false,
        success: function (msg) {
            $(this).empty();//empties the select 
            $(this).append($('<option>').text(msg).attr('value', msg));//appends to options            
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            databaseError({
                title: "Database error",
                string: "Unable to get speciality for selected individual",
                file: window.location.pathname,
                actualError: errorThrown
            });
        }
    });
});
$('#ass-seenByName,#ass-seenByName1,#ass-seenByName2')。更改(函数(){
$.ajax({
键入:“获取”,
url:“GetMDSSSpeciality.php”,
数据:{
“mds_名称”:$(this.val()
},
async:false,
成功:功能(msg){
$(this).empty();//清空选择
$(this.append($('').text(msg.attr('value',msg));//附加到选项
},
错误:函数(XMLHttpRequest、textStatus、errorshown){
数据库错误({
标题:“数据库错误”,
字符串:“无法获取所选个人的专业”,
文件:window.location.pathname,
促动器错误:错误抛出
});
}
});
});

谢谢,很抱歉我没有说清楚,但我不希望结果与选择器进入同一字段。我希望它转到选择器的3个完全不同的字段中的一个,但它转到的特定字段取决于使用的选择器。@Roy然后您需要共享如何使用单击的元素找到3个不同的字段。。。一个示例html标记将使usNever mind Arun变得更容易,我发现我可以使用$this.attr('id')获取选择器的id,然后从中使用switch语句。谢谢你的帮助,我会尽快将你的答案标记为选中!谢谢,很抱歉我没有说清楚,但我不希望结果与选择器进入同一字段。我希望它转到选择器的3个完全不同的字段中的一个,但它转到的特定字段取决于使用的选择器。@Roy然后您需要分享如何使用单击的elemen找到3个不同的字段