【问题标题】:How to give the user the ability to add another choice data?如何让用户能够添加另一个选择数据?
【发布时间】:2019-05-27 04:23:22
【问题描述】:

我有一个选择下拉菜单,用户选择其中一个选项,然后显示相关的表单输入。

这里是html:

<select id="relative" name="relative">
    <option>Select relative</option>
    <option value="father">father</option>
    <option value="mother">mother</option>
    <option value="brother">brother</option>
</select>

<div id="relative_sections">
    <div id="father">
        <input type="text" id="father_name" name="father_name" placeholder="Name" />
        <input type="email" id="father_email" name="father_email" placeholder="Email" />
        <input type="number" id="father_phone" name="father_phone" placeholder="phone" />
    </div>
    <div id="mother">
        <input type="text" id="mother_name" name="mother_name" placeholder="Name" />
        <input type="email" id="mother_email" name="mother_email" placeholder="Email" />
        <input type="number" id="mother_phone" name="mother_phone" placeholder="phone" />
    </div>
    <div id="brother">
        <input type="text" id="brother_name" name="brother_name" placeholder="Name" />
        <input type="email" id="brother_email" name="brother_email" placeholder="Email" />
        <input type="number" id="brother_phone" name="brother_phone" placeholder="phone" />
    </div>
</div>

隐藏所有部分的 CSS 代码:

#mother, 
#father, 
#brother{
    display:none;
}

显示/隐藏更改所选选项部分的 Javascript 代码:

<script type="text/javascript">
    function hideAllChildrenButOne(parentId, toRevealId) {
         var children = document.getElementById(parentId).children;
         for (var i=0; i<children.length; i++) children[i].style.display="none";
         document.getElementById(toRevealId).style.display="block";
    }

    document.getElementById('relative').addEventListener('change', function(){
        if (this.value !== '') {
            hideAllChildrenButOne('relative_sections', this.value);
        }else{
            var children = document.getElementById('relative_sections').children;
            for (var i=0; i<children.length; i++) children[i].style.display="none";
        }
    });
</script>

这是一个现场小提琴,看看发生了什么:http://jsfiddle.net/38db59cx

然后我根据所选值验证输入:

if($_POST['relative'] == 'father'){
    //Validate the inputs
}elseif($_POST['relative'] == 'mother'){
    //Validate the inputs
}elseif($_POST['relative'] == 'brother'){
    //Validate the inputs
}

我想要做的是让用户能够选择多个选项,例如('父亲'和'母亲')甚至所有选项然后我验证所有,但他必须至少填写一个选项数据。

如何做到这一点,以便用户至少选择一个选项,填写该选项的输入,并且仍然可以选择另一个选项,以便我可以验证他选择的内容?

最重要的是用户应该至少选择一个并填写相关的ata,也可以选择多个。

【问题讨论】:

    标签: javascript php css html


    【解决方案1】:

    您可以将选择框更改为多选或复选框。如果使用复选框,您可以将名称设置为 name='relative[]

    复选框:

    <label>father<input type="checkbox" name="relative[]" value="father"></label>
    <label>mother<input type="checkbox" name="relative[]" value="mother"></label>
    <label>brother<input type="checkbox" name="relative[]" value="brother"></label>
    

    多选

    &lt;select name="relative" multiple&gt;

    然后在php中你可以像这样读取数据:

    // create a container to hold the validated results
    $aRelative = array();
    
    // if post data exists proceed to check the resutls
    if(isset($_POST['relative'])){
        // loop over each post value
        foreach($_POST['relative'] as $iPos => $a){
            // do some validation on the result 
            if($a != ""){
                $aRelative[] &= $a;
            }
        }
    }
    if(!empty($aRelative)){
        //do whatever with the data
    }
    

    【讨论】:

    • issest() 应该是 isset()id(...) 应该是 if(...) 这里。你能解决这些问题吗?
    • 谢谢@FunkFortyNiner 你是反对者吗?我看不出这种方法有什么问题
    • 与方法无关,是你犯的错误。
    【解决方案2】:

    所以据我了解,您希望用户选择多个选项并希望验证它们。所以这是我要分享的一段代码。您需要在选择标签中使用属性“多个”来选择多个选项。这里是: Click here and check the code, you want to do something similar

    在 javascript 中,您可以执行此操作并为选项添加验证。

    $('#RNA').submit(function(){
         var options = $('#sampleMut > option:selected');
         if(options.length == 0){
             alert('no value selected');
             return false;
         }
    });
    

    注意:要选择多个选项,您需要执行 => 按命令/CTRL + 单击选项。 希望对您有所帮助!

    【讨论】:

    • 不一定非得是&lt;select&gt;,我想让用户知道他必须至少填写一个,如果他愿意,可以填写更多
    • 我需要一些简单的东西,用户可以理解他应该做什么
    • 这是人们常用的一种。您还可以添加自定义消息以选择至少一个字段。
    • 移动用户呢,他们怎么能选择超过 1 个选项?
    猜你喜欢
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    相关资源
    最近更新 更多