【问题标题】:Populate Select from PHP Function in Wordpress在 Wordpress 中从 PHP 函数中填充选择
【发布时间】:2015-01-04 02:39:17
【问题描述】:

我有一个如下所示的表格:

<?php
$list_id = $db_connect->get_results($db_connect->prepare('SELECT * FROM users WHERE wp_ID= %d AND svr_region=%d', $wpid, 1));
?>
<form name = "reset_data" action="<?php the_permalink(); ?>" method="post">                       
    <select id = "select_id" name="select_id" required>                               
        <option value="" > - Chose ID - </option>                                 
        <?php
        foreach ($list_id as $list_id) {
            $get_list_id = $list_id->ID;
            $get_list_name = $list_id->name;
            echo '<option value="' . $get_list_id . '" >[' . $get_list_id . '] ' . $get_list_name . '</option>';
        }
        ?>                                
    </select>                             
    <select id = "select_role" name="select_role" >                             
        <option value="" > - Chose Role - </option>                           
        <?php
        $get_role = GetRoleList($_POST['select_id']);
        echo $get_role;
        ?>                              
    </select>
    <input type="submit" value="Get Data! &#187;"/>                     
</form>

如果#select_id 有一个空值,我如何创建一个JQuery 来禁用#select_role

#select_id的值类型为Integer,选择#select_id后会执行一个PHP函数GetRoleList ( $_POST['select_id'] ),返回结果如下'&lt;option value="' . $nick_str . '" &gt;' .$nick_str. '&lt;/option&gt;'

【问题讨论】:

    标签: jquery select populate


    【解决方案1】:

    你需要$.ajax() 来支持这个,

    $(function(){
        $('#select_id').on('change',function(){
            if(this.value){
               var val=this.value;
               $.ajax({
                   url:'your php url here',
                   type:'post',
                   data:{select_id:val},
                   success:function(response){
                      $('#select_role').prop('disabled',false)
                                       .html(response);
                   }
               });
            } else {
                $('#select_role').prop('disabled',true);
            }
        });
    });
    

    您的 PHP 页面代码,

    <?php             
         $get_role = GetRoleList ( $_POST['select_id'] ) ;         
         echo $get_role ;
    ?> 
    

    如果您希望在页面加载时禁用 select-role 下拉菜单,那么您可以使用 $get_role variable 之类的,

    <select id = "select_role" name="select_role" <?php if !$get_role echo 'disabled="disabled"';?>>
       <option value="" > - Chose Role - </option>
       <?php echo $get_role ; ?>
    </select>
    

    【讨论】:

    • 感谢您的信息,但我仍然从 $get_role 得到空白值,似乎它没有加载 $_POST['select_id'],但对于禁用和启用选择它是有效的。跨度>
    • 对不起,我忘了提,这个问题在 Wordpress 上使用
    【解决方案2】:

    Ajax 是检查值并根据需要设置属性的最佳方式。 或者可以计算将附加到&lt;select id="select_id"&gt;option 标记的数量,然后如果值为1 或更少,则禁用其他select。像这样的:

    $(document).ready(function () {
            var count = $('select#select_id option').length;
            if (count <= 1) { //since 1 will be by default
                //denotes that there is only one option value
                $('#select_role').prop('disabled', true);
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 2020-06-24
      • 1970-01-01
      • 2016-04-23
      相关资源
      最近更新 更多