【问题标题】:Making certain fields in the Form read only将表单中的某些字段设为只读
【发布时间】:2012-07-29 23:19:59
【问题描述】:

我想让表单中的某些字段只读,而其他字段可编辑。我用 HTML (Read only) 阅读了这篇文章

<form action="demo_form.asp">
Country: <input type="text" name="country" value="Norway" readonly="readonly" /><br />
<input type="submit" value="Submit" />
</form>

此示例特别说明了可编辑的字段。但我正在使用 Django 表单。我无法具体执行此操作,因为该字段源自 models.py。

在使字段只读方面我有哪些选择?需要一些建议和指导...

【问题讨论】:

    标签: html django django-forms


    【解决方案1】:

    你可以使用jQuery来添加属性吗?

    $('.inputClass').prop("readonly", true);
    

    【讨论】:

    • “通常”你想使用.prop() - 答案改写以反映这一点
    • 我很想知道你是否可以使用 css 来做到这一点?
    【解决方案2】:

    在创建表单时,为您的字段添加一个额外的参数以添加只读属性:

    name = forms.CharField(widget=forms.TextInput(attrs={'readonly':'readonly'}))
    

    【讨论】:

      猜你喜欢
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      相关资源
      最近更新 更多