2013-11-22 108 views
0

我有選擇框與搜索當我添加新的元素值應該被添加到選擇框,並選擇了。選擇框附加選項不工作使用jquery

HTML

<td rowspan="3"><label>Value(s):</label> 
    <select id="selectpa_color" name="attribute_terms" tabindex="-1" class="chosen-select" multiple="" style="width: 350px; display: none;"> 
    <option value="28">Red</option> 
    <option value="29">Blue</option> 
    <option value="31">Greens</option> 
    <option value="34">light</option> 
    <option value="41">light1</option> 
    <option value="62">new color</option> 
    </select> 
    <div class="chosen-container chosen-container-multi" title="" id="selectpa_color_chosen" style=""> 
    <ul class="chosen-choices"> 
     <li class="search-field"> 
     <input type="text" style="width:25px;" autocomplete="off" class="default" value="Select Some Options" tabindex="8"> 
     </li> 
    </ul> 
    <div class="chosen-drop"> 
     <ul class="chosen-results"> 
     </ul> 
    </div> 
    </div> 
    <!--<a href="javascript:void(0);" class="select_all_attributes" id="select_all_slug-color" onclick="selectAll(select_box-color,true)">Select All</a> 
             <a href="javascript:void(0);" class="select_no" id="select_no_slug-color">Select None</a>--> 
    <input type="button" class="prompt_button" value="Add new" id="pa_color|5"> 
</td> 

的Jquery:

jQuery(".prompt_button").click(function(e){  
     var taxonomy = jQuery(this).attr('id').split("|");  
     jPrompt('Enter a name for the new attribute term::', '', 'Prompt Dialog', function(r) { 
     if(r) 
     { 
      var formData = {category_name: r,category_parent:0,taxonomy:taxonomy[0],shop_id:taxonomy[1]}; 
      e.preventDefault(); 
      jQuery.ajax({ 
       type: 'post', 
       url: baseurl+'ajax_functions/add_new_attribute_term/', 
       data: formData, 
       beforeSend: function(){ 
       }, 
       success: function(response){ 
       alert(taxonomy[0]);alert(response); 
       jQuery('select#selectpa_'+taxonomy[0]).append(response); 

       } 
      }); 
     } 

    }); 

HTML瀏覽:enter image description here

如果我添加一些其他的價值,另一個ID會追加,但沒有在選擇盒子和UL LI太..

+0

你可以發佈演示? – Nandakumar

+0

@nanda:現在它的工作:)現在面臨的另一個問題是價值被設置爲正確選擇..現在的問題是選定的值不顯示在「選擇一些選項」字段 – Aaru

+0

好帥哥......你呢得到 – Nandakumar

回答

1

這是因爲你的選擇,而構造錯誤。你有什麼將嘗試選擇#selectpa_pa_color而不是#selectpa_color

變化

jQuery('select#selectpa_'+taxonomy[0]).append(response); 

jQuery('select#select'+taxonomy[0]).append(response).trigger('chosen:updated'); 

在一個側面說明,你沒有,如果你正在使用的ID,你可以做jQuery('#select'+taxonomy[0])這是更有效的指定標記名。

+0

是的..它的工作,但價值被設置爲正確選擇..現在的問題是選擇的值不顯示在「選擇一些選項」字段:( – Aaru

+0

@AaruKarthiga你使用選擇的插件。如果是這樣 – PSL

+0

yes ..I'm使用選擇插件爲此 – Aaru