【问题标题】:select box append option not working using jquery选择框附加选项无法使用 jquery
【发布时间】:2013-11-22 04:08:21
【问题描述】:

当我添加新元素时,我有一个可搜索的选择框,该值应该添加到选择框并被选中。

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 li 也是..

【问题讨论】:

  • @nanda : 现在它可以工作了 :) 现在面临的另一个问题是值已正确设置为选定的值。现在问题是选定的值未显示在“选择一些选项”字段中
  • 好兄弟...你得到了什么

标签: javascript jquery select append prompt


【解决方案1】:

那是因为您的选择器构造错误。您将尝试选择#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]),这样更有效。

【讨论】:

  • 是的..它正在工作,但该值已正确设置为选定的值..现在问题是选定的值未显示在“选择一些选项”字段中:(
  • @AaruKarthiga 你在使用选择的插件吗?如果是这样,在添加选项后您需要刷新选择。
  • @AaruKarthiga 如果您想添加值以反映您需要触发选择的更新......请查看我的代码更新。
猜你喜欢
  • 1970-01-01
  • 2020-02-26
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-24
  • 1970-01-01
相关资源
最近更新 更多