简体   繁体   English

如果选择了多个选项,使用jQuery显示隐藏的表单元素?

[英]Using jQuery to display hidden form elements if multiple options are selected?

I've got a drop-down selection in a contact form, and want to display a few additional form elements if either of options are selected. 我在联系表单中有一个下拉选项,如果选择了其中一个选项,则想要显示一些其他表单元素。 Otherwise, the additional elements should be hidden. 否则,应隐藏其他元素。 I've been able to get this working with a single option, with the following code: 我已经能够使用单个选项使用以下代码:

$(".hidden-section").hide();
$("#contact-form select").change(function(){
    if ($(this).val() == "extra options trigger one") {
        $(".hidden-section").slideDown("fast");
    } else {
        $(".hidden-section").slideUp("fast");
    }
});

Unfortunately, I can't figure out how to get this to work if either of two options are selected. 不幸的是,如果选择了两个选项中的任何一个,我无法弄清楚如何使其工作。 The only way I've been able to get the jQuery even working is this: 我能够让jQuery工作的唯一方法是:

$(".hidden-section").hide();
$("#contact-form select").change(function(){
    if ($(this).val() == "extra options trigger one") || ($(this).val() == "extra options trigger two") {
        $(".hidden-section").slideDown("fast");
    } else {
        $(".hidden-section").slideUp("fast");
    }
});

But in this case, the hidden elements will show up once any option other than the default is selected. 但在这种情况下,一旦选择了除默认选项之外的任何选项,隐藏元素将显示。

Any ideas on a better way to go about this? 有什么想法可以更好地解决这个问题吗? Would much appreciate any advice. 非常感谢任何建议。

Thanks! 谢谢!

You've got extra parentheses on this line that shouldn't be there: 你在这一行上有额外的括号,不应该在那里:

if ($(this).val() == "extra options trigger one") || ($(this).val() == "extra options trigger two") {

Should be: 应该:

if ($(this).val() == "extra options trigger one" || $(this).val() == "extra options trigger two") {

That is, the if() statement's condition needs to be entirely enclosed within the outer parentheses, so: 也就是说, if()语句的条件需要完全包含在外括号内,因此:

if (condition1 || condition2)          // valid
if ((condition1) || (condition2))      // valid
if (condition1) || (condition2)        // NOT valid

Let me add some comments on your code. 让我在您的代码中添加一些注释。

First - don't use $(".hidden-section").hide(); 首先 - 不要使用$(".hidden-section").hide(); - you should add display:none to the .hidden-section CSS. - 你应该在.hidden-section CSS中添加display:none The reasons is that for some people the network connection is slow, and until their browser will run the javascript for hide it will take some time. 原因是对于某些人而言,网络连接速度很慢,直到他们的浏览器运行javascript for hide这需要一些时间。 During this time it will be visible. 在此期间,它将是可见的。 They will experience a flicker - something will appear and suddenly disappear. 他们会经历一个闪烁 - 一些东西会出现并突然消失。

Comment #2 - 评论#2 -

If your HTML does not load in Ajax, you should wrap it with $(function(){ /* code here */ }); 如果你的HTML没有加载到Ajax中,你应该用$(function(){ /* code here */ });包装它$(function(){ /* code here */ }); - as this will run the JavaScript only when the entire page is ready. - 因为只有在整个页面准备就绪时才会运行JavaScript。 This is required when you JavaScript refers to HTML content. 当JavaScript引用HTML内容时,这是必需的。

Comment #3 - 评论#3 -

Your code will be hard to maintain - imagine there's a third option that requires extra fields. 您的代码将难以维护 - 想象有第三个选项需要额外的字段。

You are better off adding a class on each option that requires extra fields, for example <option class="requires-extra">Option 2</option> and then your JavaScript is shorter. 你最好在每个需要额外字段的选项上添加一个类,例如<option class="requires-extra">Option 2</option>然后你的JavaScript会更短。

$(function(){
    $("select").change(function(){
        var $requiresExtra = $(this).find("option:selected").hasClass("requires-extra");
        if ( $requiresExtra ) {
            $(".hidden-section").slideDown("fast");
        } else {
            $(".hidden-section").slideUp("fast");
        }
    });
});

See my fiddle for a working example. 一下我的小提琴 ,看一个有效的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM