jQuery 使用下拉菜单在表单中显示/隐藏

发布于 2024-10-03 20:33:41 字数 3204 浏览 2 评论 0原文

我有一个表单,当有人从下拉列表中选择一个选项时,我希望显示不同的字段。由于某种原因,我的代码无法正常工作,它隐藏了可选的 div,但当我做出选择时它没有显示它们。

这是我正在使用的 jQuery(我用 $ 代替了 jQuery,因为我们正在运行另一个与 Magento 内冲突的脚本):

jQuery(document).ready(function(){
    jQuery("#ltd").change(function(){

        if (jQuery(this).val() == "1" ) {

            jQuery("#ltd").slideDown("fast"); //Slide Down Effect

        } else {

            jQuery("#ltd").slideUp("fast");    //Slide Up Effect

        }
    });

    jQuery("#partnership").change(function(){

        if (jQuery(this).val() == "1" ) {

            jQuery("#partnership").slideDown("fast"); //Slide Down Effect

        } else {

            jQuery("#partnership").slideUp("fast");    //Slide Up Effect

        }
    });

     jQuery("#sole").change(function(){

        if (jQuery(this).val() == "1" ) {

            jQuery("#sole").slideDown("fast"); //Slide Down Effect

        } else {

            jQuery("#sole").slideUp("fast");    //Slide Up Effect

        }
    });
});

我的 CSS 是:

.formhide { display:none; }
.clear-block { clear:both; }

这是以下形式的代码:

<div class="form-left-even"><span class="green">Company Type</span></div>
<div class="form-right-even"> <select>
<option value="">Please choose</option>
<option value="ltd">Limited Company</option>
<option value="partnership">Partnership</option>
<option value="sole">Sole Trader</option></select>
</div>
<div class="clear-block"></div>
<div class="formhide" id="ltd">
<!-- Limited Company -->
<div class="form-left-even">Reg Comp Name:</div>
<div class="form-right-even">
  <input type="text" name="regcompname" size="37" maxlength="100" />
</div>
<div class="clear-block"></div>
<div class="form-left-even">Reg Number:</div>
<div class="form-right-even">
  <input type="text" name="regnumber" size="37" maxlength="100" />
</div>
<div class="clear-block"></div>
</div>
<div class="formhide" id="partnership">
<!-- Partnership -->
<div class="form-left-even">Partner 1 Name:</div>
<div class="form-right-even">
  <input type="text" name="partner1" size="37" maxlength="100" />
</div>
<div class="clear-block"></div>
<div class="form-left-even">Partner 2 Name:</div>
<div class="form-right-even">
  <input type="text" name="partner2" size="37" maxlength="100" />
</div>
<div class="clear-block"></div>
<div class="form-left-even">Partner 3 Name:</div>
<div class="form-right-even">
  <input type="text" name="partner3" size="37" maxlength="100" />
</div>
<div class="clear-block"></div></div>
<div class="formhide" id="sole">
<!-- Sole Trader -->
<div class="form-left-even">Full Name:</div>
<div class="form-right-even">
  <input type="text" name="soletradername" size="37" maxlength="100" />
</div>
<div class="clear-block"></div></div>

I've got a form that I want different fields to appear when someone selects an option from a dropdown. For some reason my code isn't working, It's hidden the optional divs but it's not displaying them when I make my selection.

Here's the jQuery I'm using (I've subtituted $ for jQuery as we are running another script that conflicts within Magento):

jQuery(document).ready(function(){
    jQuery("#ltd").change(function(){

        if (jQuery(this).val() == "1" ) {

            jQuery("#ltd").slideDown("fast"); //Slide Down Effect

        } else {

            jQuery("#ltd").slideUp("fast");    //Slide Up Effect

        }
    });

    jQuery("#partnership").change(function(){

        if (jQuery(this).val() == "1" ) {

            jQuery("#partnership").slideDown("fast"); //Slide Down Effect

        } else {

            jQuery("#partnership").slideUp("fast");    //Slide Up Effect

        }
    });

     jQuery("#sole").change(function(){

        if (jQuery(this).val() == "1" ) {

            jQuery("#sole").slideDown("fast"); //Slide Down Effect

        } else {

            jQuery("#sole").slideUp("fast");    //Slide Up Effect

        }
    });
});

My CSS is:

.formhide { display:none; }
.clear-block { clear:both; }

And this is the code in the form:

<div class="form-left-even"><span class="green">Company Type</span></div>
<div class="form-right-even"> <select>
<option value="">Please choose</option>
<option value="ltd">Limited Company</option>
<option value="partnership">Partnership</option>
<option value="sole">Sole Trader</option></select>
</div>
<div class="clear-block"></div>
<div class="formhide" id="ltd">
<!-- Limited Company -->
<div class="form-left-even">Reg Comp Name:</div>
<div class="form-right-even">
  <input type="text" name="regcompname" size="37" maxlength="100" />
</div>
<div class="clear-block"></div>
<div class="form-left-even">Reg Number:</div>
<div class="form-right-even">
  <input type="text" name="regnumber" size="37" maxlength="100" />
</div>
<div class="clear-block"></div>
</div>
<div class="formhide" id="partnership">
<!-- Partnership -->
<div class="form-left-even">Partner 1 Name:</div>
<div class="form-right-even">
  <input type="text" name="partner1" size="37" maxlength="100" />
</div>
<div class="clear-block"></div>
<div class="form-left-even">Partner 2 Name:</div>
<div class="form-right-even">
  <input type="text" name="partner2" size="37" maxlength="100" />
</div>
<div class="clear-block"></div>
<div class="form-left-even">Partner 3 Name:</div>
<div class="form-right-even">
  <input type="text" name="partner3" size="37" maxlength="100" />
</div>
<div class="clear-block"></div></div>
<div class="formhide" id="sole">
<!-- Sole Trader -->
<div class="form-left-even">Full Name:</div>
<div class="form-right-even">
  <input type="text" name="soletradername" size="37" maxlength="100" />
</div>
<div class="clear-block"></div></div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ま昔日黯然 2024-10-10 20:33:41

您不是在观看更改的选择 - 您正在观看目标元素。

你想要类似的东西:

HTML

<select id='pagePartSelector'>
    <option value="part1">part1</option>
    <option value="part2">part2</option>
</select>
<div id="part1" class="pagePart">...</div>
<div id="part2" class="pagePart">...</div>

脚本:

$('#pagePartSelector').change(
 function() {
    $(".pagePart:visible").slideUp(); //slide up visible pageparts
    $("#"+$(this).val()).slideDown(); //slide down the selected page part by id
});

You're not watching the select for changes - you're watching the target elements.

You want something like:

HTML

<select id='pagePartSelector'>
    <option value="part1">part1</option>
    <option value="part2">part2</option>
</select>
<div id="part1" class="pagePart">...</div>
<div id="part2" class="pagePart">...</div>

Script:

$('#pagePartSelector').change(
 function() {
    $(".pagePart:visible").slideUp(); //slide up visible pageparts
    $("#"+$(this).val()).slideDown(); //slide down the selected page part by id
});
Bonjour°[大白 2024-10-10 20:33:41

如果我正确理解代码,似乎您正在将 .change() 附加到“#ltd”等。您需要将 .change() 事件连接到下拉列表。

If I understand the code correctly, it seems like you are attaching the .change() to the '#ltd', etc. You need to hook up your .change() event to the dropdown list.

屋檐 2024-10-10 20:33:41

正如 David Kep 所说,

jQuery("#ltd")

您需要向下拉列表提供“Id”或“class”,然后观察更改。

As David Kep says,

jQuery("#ltd")

you will need to provide "Id" or "class" to dropdown lists and then watch for the changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文