dongzhang7157 2014-08-23 03:46
浏览 52
已采纳

PHP多个后续HTML选择字段

I want to create a form with multiple follow-up select fields. This from an array that contains 3 fields: ID, Name, followingID. FollowingID's matches an ID so we can figure out the layers.

Small scale example: 0 = ID, 1 = Name, 2 = followingID

    Array
       (
        [0] => Array
            (
                [0] => 1
                [1] => Auto’s, werktuigkundigen, technici, ingenieurs
                [2] => 0
            )

        [1] => Array
            (
                [0] => 2
                [1] => Bewaking, leger, politie
                [2] => 1
            )

        [2] => Array
            (
                [0] => 3
                [1] => Bouw, montage
                [2] => 1
            )

        [3] => Array
            (
                [0] => 4
                [1] => Commercieel, winkel, inkoop en verkoop
                [2] => 2
            )

        [4] => Array
            (
                [0] => 5
                [1] => Financiën, bank, verzekering
                [2] => 2
            )

        [5] => Array
            (
                [0] => 6
                [1] => Gezondheidszorg, paramedici, laboratorium
                [2] => 3
            )

        [6] => Array
            (
                [0] => 7
                [1] => Gezondheidszorg, paramedici, laboratorium
                [2] => 4
            )

In this case level 1 -> followingID = 0

Level 2 -> followingID = 1 if chosen first option

I can not figure out a way to do this without putting everything manually in a jquery script.

  • 写回答

1条回答 默认 最新

  • doujiaozhan2413 2014-08-23 22:00
    关注

    I figured something out. It's not pretty but it works. Does anyone know a better solution?

    <script>
        $(document).ready(function() {
            var followingID = 0;
            $(".jobs").hide();
            $("#0").show();
    
            $(".jobs").change(function() {
                var id = $(this).attr("id");
                var ref = $(this).attr("value");
                $(".jobs").hide();
                $(".jobs").removeAttr("name");
                $("#0").show();
                $("#" + id).show();
                $("#" + this.value).show();
                $('option[ref="' + id + '"]').parent().show();
                if ($("#" + this.value).length == 0) {
                    $("#" + id).attr("name", "sdata[]");
                }
            });
    
        });
    </script>
    
    <?
        function renderAllJobs() {
            $jobs = getJobs();
            $followingID = -1;
            $html = '';
            foreach ($jobs as $job) {
                if ($followingID != $job[2]) {
                    if ($followingID > -1) {
                        $html .= '</select></p>';
                    }
                    $followingID = $job[2];
                    $html .= '<p><select class="jobs" ref="' . $job[2] . '" id="' . $followingID . '">
                <option value="0">Maak een keuze</option>';
                }
                $html .= '<option ref="' . $job[0] . '" value="' . $job[0] . '">' . $job[1] . '</option>';
    
            }
            $html .= '</select></p>';
            return $html;
    
        }
    
        echo renderAllJobs();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办