簡體   English   中英

jQuery將表單中的所有屬性更改為其他內容

[英]jQuery change all attributes within a form to something else

我有一個表單元素,如下所示:

@using (Html.BeginForm("Edit", "UserProfile", FormMethod.Post, new { id = "EditUserForm", @class = "form-horizontal form-bordered" }))
{

    <div class="form-group">
        @Html.LabelFor(m => m.Email, new { @class = "col-sm-4 control-label" })
        <div class="col-sm-8">
            @Html.TextBoxFor(m => m.Email, new { @class = "form-control", type = "text", @readonly = "readonly" })
        </div>
    </div><!-- form-group -->

    <div class="form-group">
        @Html.LabelFor(m => m.Name, new { @class = "col-sm-4 control-label" })
        <div class="col-sm-6">
            @Html.TextBoxFor(m => m.Name, new { @class = "form-control", type = "text", @readonly = "readonly" })
        </div>
    </div><!-- form-group -->

}

我想使用jQuery遍歷表單中的所有元素,並更改/刪除readonly屬性。 當我單擊按鈕/元素時。

我將如何去做。 我只知道如何更改我知道的單個項目的屬性?

html是For Razor視圖。

您可以使用:

$(function () {
    $('#EditUserForm [readonly]').prop('readonly', false);
});

由於您的表單中只有輸入字段。

嘗試這樣的事情:

$('input[readonly]').removeAttr('readonly');

要么

$('#EditUserForm input[readonly]').removeAttr('readonly');

工作演示

小提琴

    $(".form-group input[type=text").each(function(){
        var attr=$(this).attr('readonly');
        if(typeof attr !==typeof undefined || attr!==false)
           $(this).removeAttr('readonly');
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM