2016-11-18 97 views
0

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

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; 

回答

0

使用jQuery你可以做这样的事情:

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}); 
    } 
}) 

也可以加载选择的数据(例如,作为JSON)而不是完整的HTML,然后使用JS创建/填充选择。

欲了解更多信息具有看看jQuery docs

+0

在我的wordpress –

+0

无法正常工作,如果你需要帮助,请提供更多信息。什么不行?你尝试了什么?有没有错误?提供代码示例。 – Quagaar