简体   繁体   English

如何使用HTML切换表单 <select>控制项

[英]How to toggle forms using HTML <select> controls

I am currently developing a web application that requires me to register users according to their category. 我目前正在开发一个Web应用程序,要求我根据用户类别进行注册。 I have a combo-box in which the user can choose which type of user they are. 我有一个组合框,用户可以在其中选择自己属于哪种类型的用户。

I want to do is show a relevant form according to the user's selected value. 我要做的是根据用户选择的值显示相关表格。 I'm am using PHP for all the functionality, but I'm a newbie to jQuery. 我正在使用PHP的所有功能,但我是jQuery的新手。 Can anyone show how to implement this or else demonstrate a different way of doing this without reloading the page? 任何人都可以展示如何实现此功能,还是可以展示出另一种无需重新加载页面的方式?

You can try using a category dropdown list as shown. 您可以尝试使用如图所示的类别下拉列表。

HTML Code HTML代码

Dropdown 落下

<select id="category">
   <option value="#form1">Category 1</option>
   <option value="#form2">Category 2</option>
   <option value="#form3">Category 3</option>
</select>

Forms 形式

<form id="form1">
</form>    

<form id="form2">
</form>

<form id="form3">
</form>

jQuery Code jQuery代码

$(function(){
    var forms = $('form'); //cache all Forms
    forms.hide(); //hide initial

    $('#category').on('change', function(){
        forms.hide();                 //on change hide all forms
        var formId = $(this).val();   //get form id to show
        $(formId).show();    //find form by its id in cached forms and show.
    });

});

You can use jQuery show() and hide() functions: 您可以使用jQuery show()hide()函数:

Example: 例:

$('#form1').hide();
$('#form2').hide();
$('#form3').show();

In this example you have forms defined as follows: 在此示例中,您定义的表单如下:

<form id="form1">...</form>
<form id="form2">...</form>
<form id="form3">...</form>

You can also get fancy with it and use fadeIn() and fadeOut() if you want some effects. 如果您想要一些效果,也可以使用它,并使用fadeIn()fadeOut()

You can use jQuery toggle() function: 您可以使用jQuery toggle()函数:

$('#your_form').toggle();

From the docs: 文档:

The matched elements will be revealed or hidden immediately, with no animation, by changing the CSS display property. 通过更改CSS显示属性,匹配的元素将立即显示或隐藏,而不会产生动画。 If the element is initially displayed, it will be hidden; 如果最初显示该元素,则它将被隐藏; if hidden, it will be shown. 如果隐藏,它将显示。 The display property is saved and restored as needed. display属性将根据需要保存和恢复。 If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline. 如果一个元素的显示值为inline,则将其隐藏并显示,它将再次以inline显示。

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

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