2016-02-03 46 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