我想追加&按optgroup下拉列表删除

时间:2017-09-06 07:45:58

标签: javascript

我想追加&通过optgroup删除我可以通过使用以下js代码删除它但不能通过optgroup追加你可以检查我的js小提琴

JS代码

$j("#shipping_method optgroup[label='Free Shipping']").remove();

<fieldset>
   <select name="shipping_method" id="shipping_method" style="width:250px;" class="required-entry">
      <option value="">Please select a shipping method...</option>
      <optgroup label="Free Shipping" style="font-style:normal;">
         <option value="freeshipping_freeshipping">
            Standard Shipping (SmartPost) (2 - 7 Business Days) - $0.00                                                    
         </option>
      </optgroup>
      <optgroup label="FedEx" style="font-style:normal;">
         <option value="fedex_FEDEX_GROUND">
            Ground (2 - 4 Business Days) - $18.07                                                    
         </option>
         <option value="fedex_FEDEX_2_DAY">
            2-day (2 - 3 Business Days) - $30.62                                                    
         </option>
         <option value="fedex_STANDARD_OVERNIGHT">
            Standard Overnight (1 - 2 Business Days) - $81.34                                                    
         </option>
      </optgroup>
   </select>
</fieldset>
<a href="#" class="reset-signature" style="display: none">Clear Shipping Selection</a>
<p class="actions">
   <button id="update_shipping_method_submit" type="submit" class="button" style="display: none;"><span><span>Update Shipping Method</span></span></button>
</p>

https://jsfiddle.net/7k6evkt8/

2 个答案:

答案 0 :(得分:1)

尝试以下一个示例首先在select&amp; amp;然后删除选项&amp;当你需要附加外部克隆选项

 $("#shipping_method optgroup[label='Free Shipping']").clone("optgroup[label='Free Shipping']").insertAfter("#shipping_method_form");
    $("#shipping_method optgroup[label='Free Shipping']").remove();
    $(".box-content .fedex").appendTo("#shipping_method");

希望这有帮助!!!

答案 1 :(得分:0)

你可以试试这个:

$("#shipping_method optgroup[label='Free Shipping']").remove();
var optgroup = $('<optgroup>');
optgroup.attr('label', 'testing');
var option = $("<option></option>");
option.val('aaaa');
option.text('aaaa');
optgroup.append(option);

$("#shipping_method").append(optgroup);