jQuery 使用下拉菜单在表单中显示/隐藏
我有一个表单,当有人从下拉列表中选择一个选项时,我希望显示不同的字段。由于某种原因,我的代码无法正常工作,它隐藏了可选的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不是在观看更改的选择 - 您正在观看目标元素。
你想要类似的东西:
HTML
脚本:
You're not watching the select for changes - you're watching the target elements.
You want something like:
HTML
Script:
如果我正确理解代码,似乎您正在将 .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.
正如 David Kep 所说,
您需要向下拉列表提供“Id”或“class”,然后观察更改。
As David Kep says,
you will need to provide "Id" or "class" to dropdown lists and then watch for the changes.