gpt4 book ai didi

jquery将控件id传递给内部方法

转载 作者:行者123 更新时间:2023-12-01 08:26:14 27 4
gpt4 key购买 nike

我正在从下拉更改处理程序进行 ajax 调用(ASP.NET 3.5)并尝试将下拉 id 传递到 ajax 调用中:

$('select.facility').change(function()
{
if ($(this).val() != 0)
{
var params = new Object();
params.facilityId = $(this).val();

$.ajax(
{
type: "POST",
data: $.toJSON(params),
dataType: "json",
contentType: "application/json",
url: "SomeURL.asmx/SomeMethod",
success: function(response)
{
//this is where I need to get a hold of my dropdown,
// $(this) obviously doesn't work, using just to illustrate
$(this).closest('table').next('table')
.find('input.address').val(response.d.Address);

}

}
});

有什么优雅的方法可以做到这一点吗?

最佳答案

您可以使用this,只需添加 context option of $.ajax()所以这个很有用:

$('select.facility').change(function() {   
if ($(this).val() == 0) return;
var params = { facilityId = $(this).val() };
$.ajax({
context: this, //add this!
type: "POST",
data: $.toJSON(params),
dataType: "json",
contentType: "application/json",
url: "SomeURL.asmx/SomeMethod",
success: function(response) {
$(this).closest('table').next('table')
.find('input.address').val(response.d.Address);
}
});
});

关于jquery将控件id传递给内部方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3620165/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com