访问包含关键字的 JavaScript 对象属性

Access javascript object properties containing keywords?

本文关键字:对象 属性 JavaScript 包含 关键字 访问      更新时间:2023-09-26

我想访问带有关键字的javascript对象属性:

        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++;
        });

但因为台词:

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

这有:

 this.data-valmsg-for 

它抛出一个错误,因为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>

它抛出一个错误,因为data-valmsg-for中的"for"是一个关键字。

不,它会抛出错误,因为您正在从valmsg中减去for this.datathis.data-valmsg-for被解释为this.data - valmsg - for

您尝试解决的问题是更新 DOM 元素的数据属性。有多种方法可以做到这一点。最基本的是用getAttribute读取属性,用setAttribute设置:

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