2016-02-03 49 views
0

我想用一个关键字来访问它的JavaScript对象属性:访问包含关键字的JavaScript对象属性?

this.data-valmsg-for = this.data-valmsg-for.replace(/\d/, cloneIndex); 

这有:

this.data-valmsg-for 

 var cloneIndex = $(".clonedInput").length + 1; 
 

 
     $(document).on("click", 'button.clone', function (e) { 
 
      e.preventDefault(); 
 
      $(this).closest(".clonedInput").clone().insertAfter(".clonedInput:last").attr("id", 
 
        "clonedInput" + cloneIndex).find("[id], [name] ,[data-valmsg-for]").each(
 
        function() { 
 
         this.id = this.id.replace(/\d+$/, cloneIndex); 
 
         this.name = this.name.replace(/\d/, cloneIndex); 
 
         this.data-valmsg-for = this.data-valmsg-for.replace(/\d/, cloneIndex); 
 
        }); 
 
      cloneIndex++; 
 
     });

但由于线路

它抛出由于data-valmsg-for中的“for”是一个关键字,所以出现错误。 此外,当我改变:

this.data-valmsg-for 

要:

this['data-valmsg-for'] = this['data-valmsg-for'].replace(/\d/, cloneIndex); 

脚本抛出这个错误:

Uncaught TypeError: Cannot read property 'replace' of undefined 

此外,当我改变:

this.data-valmsg-for 

为:

this.setAttribute('data-valmsg-for',this.getAttribute('data-valmsg-for').replace(/\d/, cloneIndex)); 

脚本抛出这个错误:

Uncaught TypeError: Cannot read property 'replace' of null 

这是我的html:

<div id="clonedInput1" class="form-group clonedInput"> 
 
      <input id="CompanyId1" name="[0].CompanyId" type="hidden" value=""> 
 

 
      <label class="control-label col-md-2" for="NationalCode">کد ملی</label> 
 
      <div class="col-md-3"> 
 
       <input class="form-control text-box single-line" data-val="true" data-val-regex="فرمت کد ملی صحیح نمی باشد" data-val-regex-pattern="^[0-9]{10}$" @*data-val-remote="'کد ملی' is invalid." data-val-remote-additionalfields="*.NationalCode,*.initialNationalCode" data-val-remote-url="/Account/IsValidNationalCode"*@ data-val-required="وارد کردن کد ملی اجباریست" id="NationalCode1" name="[0].NationalCode" type="text" value=""> 
 
       <span class="field-validation-valid text-danger" data-valmsg-for="[0].NationalCode" data-valmsg-replace="true"></span> 
 
      </div> 
 

 
      <label class="control-label col-md-2" for="BirthDate">BirthDate</label> 
 
      <div class="col-md-3"> 
 
       <input class="form-control text-box single-line" data-val="true" data-val-date="The field BirthDate must be a date." data-val-required="The BirthDate field is required." id="BirthDate1" name="[0].BirthDate" type="date" value=""> 
 
       <span class="field-validation-valid text-danger" data-valmsg-for="[0].BirthDate" data-valmsg-replace="true"></span> 
 
      </div> 
 

 
</div>

+0

你必须使用'[]'符号,你必须进行检查,看的属性是否存在。 – Pointy

+1

问题不在于'for'。问题主要是'-'。 'this.data-valmsg-for'的意思是'this.data' * minus *'valmsg' * minus *'for'。 *“无法读取属性”替换“未定义”*表示该属性不存在。如果这个属性只是'for',那么'this.for'就可以很好地工作。 –

+0

鉴于'this'是一个DOM元素,您正在寻找'this.getAttribute('data-valmsg-for')'。 –

回答

0

It throws an error because "for" in data-valmsg-for is a keyword.

不,因为你是抛出一个错误减法forvalmsgthis.datathis.data-valmsg-for被解释为this.data - valmsg - for

看来你正试图解决的问题是更新DOM元素的数据属性。有多种方法可以做到这一点。最基本的一条是getAttribute读取属性,并与setAttribute设置:

this.setAttribute(
    'data-valmsg-for', 
    this.getAttribute('data-valmsg-for').replace(/\d/, cloneIndex) 
); 
+0

或者,使用['this.dataset.valmsgFor'](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset):-) – Bergi

+1

那些奇特的新API ... 。 –

+0

脚本抛出此错误:未捕获到的字符类型错误: 无法读取属性'替换'null – Hamed