这是从控制台看起来的元素:

<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();

    }

    });
  });

最佳答案

以下内容不起作用:

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

关于jquery - 为什么jQuery将此属性显示为undefined?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35465296/

10-11 07:05