-1

I have a form with form elements such as follows:

@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 -->

}

I want to use jquery to loop through all the elements within the form and to change / remove the readonly Attribute. When I click a button / element.

how would I go about doing this. I only know how to change the attribute of a single item which Id I know?

html is For Razor view.

4

3 回答 3

2

您可以使用:

$(function () {
    $('#EditUserForm [readonly]').prop('readonly', false);
});
于 2014-09-30T11:59:26.853 回答
1

因为您的表单中只有输入字段。

尝试这样的事情:

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

或者

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

工作演示

于 2014-09-30T11:58:43.017 回答
1

小提琴

    $(".form-group input[type=text").each(function(){
        var attr=$(this).attr('readonly');
        if(typeof attr !==typeof undefined || attr!==false)
           $(this).removeAttr('readonly');
    });
于 2014-09-30T12:02:18.083 回答