如何使用django jQuery在django的change_form.html中找到字段集ID [英] how to find the fieldset id in django's change_form.html using django jQuery
问题描述
change_form.html中有许多字段集.基于单击功能,我希望隐藏/显示某些字段集.如何获取字段集ID?有1个以上的字段集.
I have many fieldsets in the change_form.html. Based on a click function I wish to hide/display some of the fieldsets. How do I get the fieldset id? There are more than 1 fieldsets.
在javascript中 例如. $('fieldset')[1] .hidden = true; //根据某些情况为假
In javascript eg. $('fieldset')[1].hidden =true; //false based on some condition
我想要与上述声明等效的django jQuery.
I want a django jQuery equivalent of the above statement.
我已经尝试过,$(#fieldset")1 = true; 还尝试了$("div.form-row.field1.field2").hidden = true;
I have tried, $("#fieldset")1 = true; Also tried $("div.form-row.field1.field2").hidden = true;
我只想按以下方式使用django.jQuery.如何访问字段集1、2和显示/隐藏功能.
I want to use django.jQuery only as below. How to access the fieldset 1, 2 and the show/hide functionality.
(function($){
$(document).ready(function($){
//function to hide/show the fieldset related to POP3_status
// if it is checked, show fieldset[1] and fieldset[2]
//if it is not checked, hide fieldset[1], fieldset[2]
$("#id_pop3_status").click(function(){
var checked = $("#id_pop3_status").is(':checked');
//not a POP3 account
if (!checked){
alert('not clicked');
$("#fieldset")[1].hide();
}
//if the POP3_status is checked i.e it is POP3 account
else {
alert('clicked');
$("#fieldset")[1].show();
}
});
});
})(django.jQuery);
推荐答案
您尝试过的所有方法都不是有效的jQuery:
Neither of the things you tried were valid jQuery:
$('fieldset')[1].hidden = true
1)在为jQuery对象集添加下标时,您会得到一个常规的javascript对象,因此实际上您需要执行类似$($('fieldset')[1])
的操作将其转换为jQuery对象. 2)hidden
不是有效的属性;您需要.hide()
/.show()
或类似.css('display', 'none')
的东西.
1) When you subscript a jQuery object set, you get back a regular javascript object, so you actually need to do something like $($('fieldset')[1])
to turn it back into a jQuery object. 2) hidden
is not a valid property; you need either .hide()
/.show()
or something like .css('display', 'none')
.
$("div.form-row.field1.field2").hidden = True
1)实际上,这将选择具有类"form-row"以及"field1",""和"""field2"的类的div
,它们将不匹配任何内容.您要查找的是$('div.form-row.field1, div.form-row.field2')
. 2)关于hidden
不是上面的有效属性的相同内容适用
1) This will actually select div
s that have the class "form-row" as well as "field1" and "field2", which won't match anything. What you're looking for is $('div.form-row.field1, div.form-row.field2')
. 2) The same bit about hidden
not being a valid property above applies
这篇关于如何使用django jQuery在django的change_form.html中找到字段集ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!