[英]How do I select an HTML attribute with jQuery?
我向文本框添加了自定义属性:
<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);
});
});
但到目前为止,它不工作,我试着输入我的jQuery选择我DidItWork
可变几种不同的方法,但不是显示SomeValue
像我希望,我的警告显示[object Object]
或[undefined]
。 我尝试了在结尾处不带.val()
情况,但这使警报显示文本框中的值,而不是MyCustomAttribute
的值。
无论如何,我不确定我弄错了语法的哪一部分,有人可以帮忙吗?
根据此链接 ,您可以在jQuery中使用attr()来操纵HTML属性。
您应该为文本框分配一个ID,并使用jQuery获取该元素并使用attr()修改其属性。
示例代码行是:
$('#textBox').attr('MyCustomAttribute', 'SomeNewValue');
尝试按如下所示的类查找asp.TextBox:
<asp.TextBox class="tb" MyCustomAttribute="SomeValue"><asp.TextBox>
.change(function () {
$.ajax({
success: function (response) {
if (response.d == "SUCCESS") {
var DidItWork = $(".tb").attr("MyCustomAttribute");
alert(DidItWork);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.