Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jQuery选择HTML属性?_Javascript_Jquery_Html_Asp.net_Ajax - Fatal编程技术网

Javascript 如何使用jQuery选择HTML属性?

Javascript 如何使用jQuery选择HTML属性?,javascript,jquery,html,asp.net,ajax,Javascript,Jquery,Html,Asp.net,Ajax,我向文本框添加了一个自定义属性: <asp.TextBox MyCustomAttribute="SomeValue"><asp.TextBox> 但到目前为止它还不起作用,我试着用几种不同的方式为我的DidItWork变量键入我的jQuery select,但我的警报并没有像我希望的那样显示SomeValue,而是显示[object object]或[undefined]。最后,我尝试了使用和不使用.val()的方法,但这会使警报显示文本框中的值,而不是MyCusto

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

<asp.TextBox MyCustomAttribute="SomeValue"><asp.TextBox>
但到目前为止它还不起作用,我试着用几种不同的方式为我的
DidItWork
变量键入我的jQuery select,但我的警报并没有像我希望的那样显示
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);
  });
});

.改变(功能){
$.ajax({
成功:功能(响应){
if(response.d==“SUCCESS”){
var DidItWork=$(“.tb”).attr(“MyCustomAttribute”);
警惕(说教);
});
});

但是您的didItWork变量被设置为包装您选择的DOM节点的jQuery对象。要显示myCustomAttribute的值,您需要console.log(didItWork.attr(“myCustomAttribute”));元素是否在响应中?如果是,那么它不会在文档中显示任何内容。如果您试图找到获取属性的元素,那么为什么不使用它?您已经有了对它的引用。它有一个ID,但页面上可能有几十个这样的文本框。我正在尝试使用MyCustoMattAttribute作为唯一标识它有一个类,但就像我对Clink123的回复一样,我无法找到我试图单独按类或ID选择的文本框,我需要MyCustomAttribute使其唯一,因为页面上可能有几十个这样的文本框
<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);
  });
});