繁体   English   中英

选择框附加选项无法使用jQuery

[英]select box append option not working using 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视图: 在此处输入图片说明

如果我将其他值附加到另一个ID,它将附加,但在选择框和ul中都没有。

那是由于您的选择器,而构造错误。 您将尝试选择#selectpa_pa_color而不是#selectpa_color

更改

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

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

附带说明一下,如果您使用的是id,则不必指定tagName,而只需执行效率更高的jQuery('#select'+taxonomy[0])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM