javascript - 如何使用 jQuery 选择 HTML 属性?

标签 javascript jquery html asp.net ajax

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

<asp.TextBox MyCustomAttribute="SomeValue"><asp.TextBox>

我想从 AJAX 内部访问该值如果可能的话,成功函数。

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

我正在使用AJAX jQuery 内部 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);
  });
});

但到目前为止它还不起作用,我已经尝试为我的 DidItWork 输入我的 jQuery 选择变量有几种不同的方式,但不是显示 SomeValue正如我所希望的,我的警报显示 [object Object][undefined] 。我尝试过使用和不使用 .val()最后,但这使得警报显示文本框中的值,而不是 MyCustomAttribute 的值.

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

最佳答案

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

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

示例代码行是:

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

关于javascript - 如何使用 jQuery 选择 HTML 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53343478/

相关文章:

Javascript 窗口未定义

javascript - 在 $ 'Ng-If' ).change 或 angularfire $add 上使用 "breaks"('#x' 程序

javascript - 当我们在带有打印窗口的屏幕中的任意位置单击时,引导模式背景不会消失

php - 一张一张上传图片

javascript - 在 javascript 中使用 A-frame img 资源

javascript - 让 jQuery 'start with' 选择器与构造的对象一起工作

javascript - jQuery 表单验证和提交问题

javascript - jQuery - child 的麻烦

javascript - href 菜单栏上的下拉按钮

javascript - 如何获取特定标题之后的表格