如何隐藏隐藏字段的标签
我一直在尝试使用 Django api 隐藏模型类中定义的隐藏字段的标签。模型的CRUD操作由Django admin管理。
我的模型是
class RackForm(django.forms.ModelForm):
def __init__(self, *args, **kwargs):
self.fields['racktypeid'].widget = \
forms.HiddenInput(attrs={'styles': 'display:none;'})
..... other initialization
生成的 html 源如下所示
<div class="form-row racktypeid">
<div>
**<label for="id_racktypeid" class="required">Rack Type:</label>**
**<input styles="display:none;" name="racktypeid" value="3" onchange="changeRackType(this);" type="hidden" id="id_racktypeid" />**
<p class="help">The physical type of rack as defined in the rack type list</p>
</div>
</div>
该字段已被隐藏,但未隐藏关联的标签。我什至尝试过使用 jQuery。我的语法可能有问题。如果我是的话,如果我错了,请纠正我。
$(document).ready(function() {
// Hide label for RackForm rack type id label.
$('#id_racktypeid, label[for="#id_racktypeid"]').hide()
$('#id_racktypeid, label[for="#id_racktypeid"]').parent().hide()
});
});
没有一个选项成功。我需要做什么来隐藏隐藏字段的标签吗?
更新:最终我选择隐藏整个 div 标签。
I have been trying to hide the label of the hidden field defined in a model class using Django api. The CRUD operations of the model are managed by Django admin.
My model is
class RackForm(django.forms.ModelForm):
def __init__(self, *args, **kwargs):
self.fields['racktypeid'].widget = \
forms.HiddenInput(attrs={'styles': 'display:none;'})
..... other initialization
The generated html source is given below
<div class="form-row racktypeid">
<div>
**<label for="id_racktypeid" class="required">Rack Type:</label>**
**<input styles="display:none;" name="racktypeid" value="3" onchange="changeRackType(this);" type="hidden" id="id_racktypeid" />**
<p class="help">The physical type of rack as defined in the rack type list</p>
</div>
</div>
The field has been hidden but not the associated label. I have even tried using jQuery for it. I may be wrong with the syntax. If I am, please correct me if I am wrong.
$(document).ready(function() {
// Hide label for RackForm rack type id label.
$('#id_racktypeid, label[for="#id_racktypeid"]').hide()
$('#id_racktypeid, label[for="#id_racktypeid"]').parent().hide()
});
});
None of the options succeed. Is there anything I need to do hide the label of the hidden field?
Update: Eventually I chose to hide the entire div tag.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题源于使用
{{ form.as_p }}
或其他一些快捷函数来呈现表单。自己渲染表单就不会出现这个问题。另外,在 Django 1.3 中,有一种更简单的方法来更改字段的小部件,请参阅 http://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets< /a>
Your problem stems from using
{{ form.as_p }}
or some other other shortcut function to render your form. Render the form yourself and you won't have this problem.Also, in Django 1.3 there is an easier way to change the widget for a field, see http://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets