为什么 jQuery 将此属性显示为未定义?

Why is jQuery showing this attribute as undefined?

这是元素在控制台中的样子:

<input type="text" size="20" tabindex="3" id="txtR7163776" name="txtR7163776" selectorviewid="4149027" viewpageid="4877455" class="requiredInput" onfocus="new AutoSuggestControl(this, 4148821, 4149027);" matchfield="name" matchrule="ST" suggtype="rObj" ismultiple="false" autocomplete="off" filtername="R511444" filtervalue="-1">

我正在尝试删除属性 filternamefiltervalue

我的代码:

$(document).ready(function(){
   var input = $("#txtR7163776");
   var attrA = input.attr("filtername");
   var attrB = input.attr("filtervalue");
   console.log(attrA, attrB);
   input.removeAttr("filtername");
   input.removeAttr("filtervalue");
});

attrAattrB 通过控制台显示为未定义...我在这里缺少什么?

更新代码:

$(document).ready(function(){
  var input = $("#txtR7163776");
  console.log(input.length); //1

  var attrA = input.attr("filtername");
  var attrB = input.attr("filtervalue");
  console.log(attrA, attrB); //showing undefined undefined in the console    

  input.removeAttr("filtername");
  input.removeAttr("filtervalue");

    var payroll = $("div[name='Payroll']");

var timekeep = $("div[name='Timekeeping']"); 

var benefits = $("div[name='Benefits']");

var HRIS = $("div[name='HRIS']");

payroll.hide();
timekeep.hide();
benefits.hide();
HRIS.hide();


console.log("A");
$("select[name='oppty_type']").click(function(){
    console.log("B");
    var isPay = false;
    var isTime = false;
    var isBene = false;
    var isHris = false;
    var isPremium = false;
    var isPremiumPlus = false;
    payroll.hide(); 
    timekeep.hide();
        benefits.hide();
    HRIS.hide();


    var multi = [];
    multi = $("select[name='oppty_type']").val(); 
    console.log(multi);
    for(var a = 0; a < multi.length; a++){
        if(multi[a] == 4143371){
            isPay = true;
        }
        if(multi[a] == 4143372){
            isTime = true;
        }
        if(multi[a] == 4143370){
            isHris = true;
        }
        if(multi[a] == 7457612){
            isBene = true;
        }
        if(multi[a] == 58318982){
            isPremium = true;
        }
        if(multi[a] == 58318983){
            isPremiumPlus = true;
        }
    }

    if(isPay){ //payroll

        payroll.show(); 


    }
    if(isTime){ //timekeeping

        timekeep.show();


    }
    if(isHris){ //HRIS

        HRIS.show();


    }
    if(isBene){ //Benefits

        benefits.show();



    }
    if(isPremium){ //bundle premium

        payroll.show(); 


        timekeep.show();


        benefits.show();



    }
    if(isPremiumPlus){ //bundle premium plus 

        payroll.show(); 


        timekeep.show();


        benefits.show();


        HRIS.show();

    }

    });
  });

您正在从 attrAattrB 而不是 input 中删除属性。试试关注

input.removeAttr("filtername");
input.removeAttr("filtervalue");

以下将不起作用:

attrA.removeAttr("filtername");
attrB.removeAttr("filtervalue");

因为你在attrAattrB上操作它们,它们是string。上面的语句大致翻译成一个意思少:

"R511444".removeAttr("filtername");
"-1".removeAttr("filtername");

您必须做的事情:

input.removeAttr("filtername");
input.removeAttr("filtervalue");

但在控制台中我得到了两个 attr 值:

输出:http://jsbin.com/hadukojifi/edit?html,js,console,output

尝试在输入变量声明中使用索引 0

var input = $("#txtR7163776")[0]

也许您的 jquery 版本正在返回一个数组而您错过了..在我的 jquery 版本中您的代码工作正常,