【发布时间】:2019-04-19 23:22:26
【问题描述】:
我向文本框添加了一个自定义属性:
<asp.TextBox MyCustomAttribute="SomeValue"><asp.TextBox>
如果可能,我想从 AJAX 成功函数中访问该值。
另外请注意,我已经省略了与我的问题无关的所有属性和参数。
我在 jQuery change() 函数中使用 AJAX,因此当用户更新文本框中的值时,它会更新数据库而不发回,而我已经完成了很多工作。
一旦进入change() 函数的上下文,我将MyCustomAttribute 的值设置为一个变量,这样我就可以(理论上)从AJAX 成功函数内部访问它:
.change(function () {
var MyCustomAttribute = $(this).attr("MyCustomAttribute");
$.ajax({
success: function (response) {
if (response.d == "SUCCESS") {
// here I just want to see if I can access the value
// of MyCustomAttribute so I'm trying to set it to a
// variable and do an alert()
var DidItWork = $("input[MyCustomAttribute='" + MyCustomAttribute + "']");
alert(DidItWork);
});
});
但到目前为止它不起作用,我尝试用几种不同的方式为我的 DidItWork 变量键入我的 jQuery 选择,但不是像我希望的那样显示 SomeValue,我的警报显示 [object Object] 或 @987654332 @。我在最后尝试了使用和不使用.val(),但这会使警报显示文本框中的值,而不是MyCustomAttribute 的值。
无论如何我不确定语法的哪一部分我错了,有人可以帮忙吗?
【问题讨论】:
-
但是您的 didItWork 变量设置为包装您选择的 DOM 节点的 jQuery 对象。要显示 myCustomAttribute 的值,您可以使用 console.log(DidItWork.attr("MyCustomAttribute") );
-
响应中的元素吗?如果是,那么它不会在文档中找到任何内容。如果您试图找到首先获得 attr 的元素,那么您为什么不使用它?你已经有了它的引用。
标签: javascript jquery html asp.net ajax