繁体   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