在jquery中设置初始状态?
我有一个表格,其中包含后续问题,只有当他们以某种方式回答特定的其他问题时才应显示这些问题。如果我从空白表格开始,我现在设置的方式效果很好。然而,当用户回来编辑表单并加载复选框是否从数据库中预先检查(通过 php)时,我的设计就崩溃了。
因此,我需要一些关于如何最好地重构我的 jquery 代码的建议或建议,以便它在表单加载时显示和隐藏正确的元素。
现在,我有许多用于复选框或选项按钮的 .change() 函数,这些函数会导致其他(或多个)元素隐藏和/或显示,如下所示:
$("#elt_prev_divorced").change(function () {
if ($(this).is(":checked")) {
$("#questions_divorced").show("slow");
} else {
$("#questions_divorced").hide("slow");
}
});
$("input").change(function () {
var selected_item = $(this).attr('id');
switch (selected_item) {
case "elt_married":
$("#elt_inrel_yes").attr('checked', true);
$("#elt_reldes_married").attr('checked', true);
$("#li_spouse_or_partner").show("slow");
$("#li_marriages").show("slow");
break;
case ...
}
然后我有一个名为“initial_hidden”的 CSS 类如果表单为空白,则为表单加载时不应显示的所有项目设置。在我的文档准备功能中,我用 CSS 标签隐藏了所有内容。
(function($) {
$(".initially_hidden").hide();
})(jQuery);
然而,在隐藏这些元素之后,我现在需要执行所有这些 .change() 函数的等效操作来处理加载现有数据的表单。我知道我可以将更改函数复制并粘贴到准备执行此操作的文档中,但除了我这样做的风格肯定是可怕的之外,我更希望最大限度地减少重复代码。那么我怎样才能最好/最有效地做到这一点呢?我对 JQuery 有点陌生,所以我不确定这里应该采取什么方向。谢谢你!
I have a form which has follow-up questions that should be shown only shown if they answer a particular other question a certain way. The way I have it set up right now works great if I start out with a blank form. However, when the user comes back to edit the form and its loading whether the checkboxes are pre-checked from the database (via php), my design is breaking down.
So I need some advice or suggestions on how to best restructure my jquery code so that it will show and hide the correct elements when the form loads.
Right now, I have a number of .change() functions for checkboxes or option buttons that cause other (or multiple) elements to hide and/or show, such as the following:
$("#elt_prev_divorced").change(function () {
if ($(this).is(":checked")) {
$("#questions_divorced").show("slow");
} else {
$("#questions_divorced").hide("slow");
}
});
$("input").change(function () {
var selected_item = $(this).attr('id');
switch (selected_item) {
case "elt_married":
$("#elt_inrel_yes").attr('checked', true);
$("#elt_reldes_married").attr('checked', true);
$("#li_spouse_or_partner").show("slow");
$("#li_marriages").show("slow");
break;
case ...
}
And then I have a CSS class called "initially_hidden" that is set for all items that shouldn't be shown when the form loads if the form is blank. In my document ready function, I hide everything with that CSS tag.
(function($) {
$(".initially_hidden").hide();
})(jQuery);
After I hide those elements, however, I now need to do the equivalent of all of those .change() functions to handle the form being loaded with existing data. I know I could copy and paste my change functions into document ready to do this, but aside from being certainly horrific style were I do that, I would much prefer to minimize duplicate code. So how could I do this best/efficiently? I am somewhat new to JQuery, so I'm not sure what direction to take here. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在文档就绪函数中,调用
$("#elt_prev_divorced").change()
来执行您已注册的处理程序。In your document-ready function, call
$("#elt_prev_divorced").change()
to execute the handler you've already registered.首先,在 css
.initially_hidden
中,可以在样式表或页内样式中设置display:none
。 jQuery 将设置display:block
或内联样式中的任何内容。因此,我不认为使用initially_hidden
类对元素调用hide()
有何意义。我要做的就是在服务器端处理这个问题,并在所有不应该显示的元素上设置
initially_hidden
,并让我的CSS隐藏这些元素。这样,所有应该显示的元素都在表单上,而所有不应该显示的元素都被隐藏了。如果您有一个复选框检查将使其他元素可见,我建议您执行两件事中的一件。
遍历与复选框相关的 dom 以确定应显示哪个元素,因此无需硬编码 id。 这个小提琴有一个类似的例子。
向您的复选框添加一个自定义属性,其中包含要显示的 div/元素的 id:
< strong>HTML
jQuery
希望这有帮助并且有意义。
Firstly in your css
.initially_hidden
can havedisplay:none
set in the stylesheet or the in-page style. jQuery will setdisplay:block
or whatever in the inline style. So I don't see the sense of callinghide()
on your elements with the classinitially_hidden
.What I would do is handle this on the server side and set
initially_hidden
on all elements that should not be displayed and let my css hide those elements.This way, all the elements are on the form that should be displayed and all that should not be displayed are hidden. If you have a checkbox checking which will make other elements visible, I would suggest you do 1 of 2 things.
Traverse the dom relative to your checkbox to determine which element should be displayed, hence no need to hardcode an id. This fiddle has an example of something like this.
Add a custom attribute to your checkbox which contains the id for the div/element you want to display:
HTML
jQuery
Hope this helps and makes sense.