2016-11-18 8 views
0

Select2でAjax Load Selectをロードするにはどうすればよいですか?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

で見て、私のワードプレス –

+0

では動作しませんあなたが助けが必要な場合はより多くの情報を提供してください。何がうまくいかない?何を試しましたか?何か間違いはありますか?コードサンプルを提供する。 – Quagaar

関連する問題