如何使用jQuery选择HTML属性?

时间:2018-11-16 18:28:22

标签: javascript jquery html asp.net ajax

我向文本框添加了自定义属性:

MERGE INTO STATUS as S
    USING (VALUES(
                CAST(:ORDER as INT),
                CAST(:STATUS as VARCHAR),
                CAST(:IS_ACTIVE as DECIMAL(1,0)),
                CAST(:DATE_UPDATED as DATE)
                )
            )
    AS O(order, status, is_active, date_updated)    
    ON o.order = S.order_id 
WHEN MATCHED THEN UPDATE SET order_status = status, is_active = is_active, date_updated = date_updated
WHEN NOT MATCHED THEN INSERT VALUES(order, status, is_active, date_updated)

如果可能的话,我想从<asp.TextBox MyCustomAttribute="SomeValue"><asp.TextBox> 成功函数内部访问该值。

还请注意,我忽略了与我的问题无关的所有属性和参数。

我在jQuery AJAX函数内使用AJAX,因此,当用户更新文本框中的值时,它将更新数据库而不会回发,并且我已经做了很多工作。

一旦我进入change()函数的上下文中,便将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 select,但是我的警报显示为{{ 1}}或DidItWork。我尝试了在结尾处不带SomeValue的情况,但这使警报显示的是文本框中的值,而不是[object Object]的值。

无论如何,我不确定我弄错了语法的哪一部分,有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

根据this link,您可以在jQuery中使用attr()来操纵HTML属性。

您应该为文本框分配一个ID,并使用jQuery获取该元素并使用attr()修改其属性。

示例代码行是:

$('#textBox').attr('MyCustomAttribute', 'SomeNewValue');

答案 1 :(得分:0)

尝试按这样的类查找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);
  });
});