简体   繁体   English

jQuery将表单中的所有属性更改为其他内容

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

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. 我想使用jQuery遍历表单中的所有元素,并更改/删除readonly属性。 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. html是For Razor视图。

You can use: 您可以使用:

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

Since there is only input fields in your form. 由于您的表单中只有输入字段。

Try Something like this: 尝试这样的事情:

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

OR 要么

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

Working Demo 工作演示

FIDDLE 小提琴

    $(".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