driuwt9557 2017-04-19 14:41
浏览 50
已采纳

下拉列表仅保存然后显示第一个值

So to continue my last question (link). I've finally got that sorted out (with help), but now the value of the name is only the first value of the drop down list.

A brief explanation, I have 2 drop down menu's and when you select a option from one (A) the other drop down menu is updated (B). I know it has something to do with an array but I can't figure this out.

Here are my files.

HTML

<select id="main_cat" name="main_cat">
    <option selected="-1" select="selected">Select something</option>

    <?php 
        $sttub = str_replace('&', 'en', $stub);
        $q = $row["Categorie"];
            echo "
            <option class='dropdownmenu_A' value='".$sttub."' name='".$q."'>"
                .$row["Categorie"]."
                    <span style='font-size:1.2rem;color:#F8F8F8;'>
                        (" . $row['x'] .  ")
                    </span>
            </option>
        ";
    }}?>
</select>
<select name="sub_cat" id="sub_cat" disabled="disabled"></select>

JavaScript

$(function(){
    $('#main_cat').change(function(){
            var $mainCat=$('#main_cat').val();
            var $mainName = $(".dropdownmenu_A").attr("name");
            // call ajax
             $("#sub_cat").empty();
                $.ajax({
                url:"<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",     
                type:'POST',
                data:'action=my_special_ajax_call&main_catid=' + $mainCat + '&main_name=' + $mainName,

                success:function(results)
                       {
                        //  alert(results);
                        console.log($mainCat,$mainName);
                        $("#sub_cat").removeAttr("disabled");       
                          $("#sub_cat").append(results);
                        }
                   });
          }
    );
});     

function.php

function implement_ajax() {
if(isset($_POST['main_catid']))
            {
            $q = $_POST['main_catid'];
            $x = $_POST['main_name'];
            echo '<option value="-1" selected="selected">'.$x.'</option>'.$option;
            die();
            } // end if
}

I have tried using <select id="main_cat" name="main_cat[]"> like I found on google but this didn't work. Using $x[] = $_POST['main_name']; just echos the word Array. How do I get this to work and display the correct option that is selected and not just the first every time.

To be clear, here are my drop down menu's (sometimes my brain goes faster then I can type, so I hope it's clear).

select{height:30px}
<select id="main_cat" name="main_cat">
    <option selected="-1" select="selected">Select something</option>
  <option class='dropdownmenu_A' value='option-1' name='Option 1'>
  <option class='dropdownmenu_A' value='option-2' name='Option 2'>
  <option class='dropdownmenu_A' value='option-2' name='Option 2'>
</select>
<select id="sub_cat" name="sub_cat">
    <option selected="-1" select="selected">Select something</option>
  <option class='dropdownmenu_B' value='sub-option-1' name='Sub Option 1'>
  <option class='dropdownmenu_B' value='sub-option-2' name='Sub Option 2'>
  <option class='dropdownmenu_B' value='sub-option-2' name='Sub Option 2'>
</select>

So right now if I select Option 1 from dropdownmenu_A it only echo's the first value from dropdownmenu_A to dropdownmenu_B and not Option 2 or Option 3.

</div>
  • 写回答

2条回答 默认 最新

  • duanluan3651 2017-04-19 15:13
    关注

    1- You can't have <span/> tags inside <option/> tags as the latter cannot have any child elements.

    2- <option/> doesn't have a name attribute. If you want to create a custom attribute, use HTML5 data attributes. That's what they are for.

    3- printf is your new friend.

    printf('<option class="dropdownmenu_A" value="%s" data-name="%s">%s (%s)</option>', $sttub, $q, $row["Categorie"], $row['x']);
    

    4- I believe the problem is $(".dropdownmenu_A").attr("name") as this would always pull the same name and not the selected name. In your particular case, I would do

    $(function(){
        $('#main_cat').change(function(){
            var $option = $(this).find('option:selected'),
                id = $option.val(),
                name = $option.data('name');
    
            // open your browser's console log and ensure that you get the correct values
            console.log(id, name);
    
            $("#sub_cat").empty();
    
            // call ajax
            $.ajax({
                url: "<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",     
                type:'POST',
                data: {
                    action: 'my_special_ajax_call',
                    main_catid: id,
                    main_name: name
                },
                success: function (results) {
                    $("#sub_cat").removeAttr('disabled').html(results);
                }
            });
        });
    }); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog