douduandiao1368 2016-11-18 20:44
浏览 250

如何使用Select2进行Ajax加载选择?

How can I load Ajax Load Select With Select2?

mycode: (Wordpress Metabox)

$selectedArtist = get_post_meta($post->ID,'artist_id',true);

if($artists->have_posts()):
echo '<span>Please Select Artist : </span>';

    echo '<select name="artist_id[]" class="skant-select" style="width: 90%;" multiple="multiple">';
    echo '<option value="0">-- Please Select Artist --</option>';

        while ($artists->have_posts()):
            $artists->the_post();
            echo '<option value="'.$artists->post->ID.'" '.selected(true,in_array($artists->post->ID,$selectedArtist)).' >'.get_the_title().'</option>';

        endwhile;
            echo '</select>';
            wp_reset_postdata();

endif;
  • 写回答

1条回答 默认 最新

  • duanfei8149 2016-11-18 21:07
    关注

    Using jQuery you could do something like this:

    HTML

    <select id="select">
        <option value="">Please select</option>
        <option value="v1">Value #1</option>
        <option value="v3">Value #2</option>
        <option value="v4">Value #3</option>
    </select>
    <div id="select2_container">
    </div>
    

    JavaScript

    // Register onchange listener for 1st select
    $('#select').change(function() {
        if (this.value) {
            // Load the HTML of the 2nd select from the server
            // and place it in the container
            $('#select2_container').load('/the/url', { value: this.value});
        }
    })
    

    It would be also possible to just load the data for the select (e.g. as JSON) and not the complete HTML and then create/populate the select using JS.

    For more info have a look at the jQuery docs

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度