简体   繁体   English

Html 中的条件格式?

[英]Conditional Formatting in Html?

I am writing a website and I am currently working on the sign up page.我正在写一个网站,我目前正在注册页面上工作。 I have a drop down box and I want to have that drop down box open different sign up information for each one.我有一个下拉框,我希望该下拉框为每个下拉框打开不同的注册信息。 For example: If they picked prime user it would change the sign up information they needed from just username and password to username, password, credit card number, and telephone number .例如:如果他们选择了主要用户,它将把他们需要的注册信息从用户名和密码更改为用户名、密码、信用卡号和电话号码。 OR if they picked partial user from the drop down list it would ask for username password and telephone.或者,如果他们从下拉列表中选择了部分用户,它会要求输入用户名密码和电话。 Any clue how to do this in HTML or any other computer language?任何线索如何在 HTML 或任何其他计算机语言中执行此操作?

Assuming html like this:假设 html 是这样的:

Type:<br>
one <input type="radio" name="type" id="type-1" value="1" /><br>
two <input type="radio" name="type" id="type-2" value="2" />
<hr>
<form action="." METHOD="POST">
    <input class="second" type="text" name="name" id="name" value="name" />
    <input class="second" type="text" name="email" id="email" value="email" />
    <input class="second" type="text" name="credit-card" id="credit-card" value="credit card" />
</form>

And css like this: (to hide all the form fields except for type choice)和这样的 css: (隐藏除类型选择之外的所有表单字段)

.second{
    display:none   
}

You can use jQuery javascript library to show/hide the required form fields dynamically like this:您可以使用 jQuery javascript 库来动态显示/隐藏所需的表单字段,如下所示:

// when type radio button is pressed
$('#type-1,#type-2').change(function(){

    // hide all form fields
    $('.second').hide()

    // if type is 1
    if($('#type-1:checked').length){

        // show name and email fields
        $('#name,#email').show()

    // else if type is 2
    }else if($('#type-2:checked').length){

        // show name, email and credit-card fields
        $('#name,#email,#credit-card').show()

    }     
})

This is demonstrated here: http://jsfiddle.net/rBvLA/这在这里演示: http : //jsfiddle.net/rBvLA/

The result must be processed by server side script using any language you choose.结果必须由服务器端脚本使用您选择的任何语言进行处理。

You might want to look into any of the many fine server side tools available, such as asp.net, php, etc... you could also use javascript.您可能想要查看许多可用的优秀服务器端工具中的任何一个,例如 asp.net、php 等……您也可以使用 javascript。

For instance, using JavaScript, you could have an event fire when they change the drop down and in the code for that event handler, you could modify the DOM in such a way as to display the appropriate form elements for each selection.例如,使用 JavaScript,您可以在更改下拉列表时触发事件,并且在该事件处理程序的代码中,您可以修改 DOM,以便为每个选择显示适当的表单元素。

another jQuery solution:另一个 jQuery 解决方案:

Live Demo现场演示

$('#reg_type input[type=radio]').change(function() {
    var type = $(this).attr('class');
    $('#reg_fields div').each(function() {
        if ($(this).hasClass(type)) {
            $(this).show().removeAttr('disabled');
        } else {
            $(this).hide().attr('disabled','disabled');
        }
    });
});

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

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