我想通过 optgroup 添加和删除我可以使用下面的 js 代码删除它,但不能通过 optgroup 添加,你可以检查我的 js fiddle
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/
最佳答案
尝试下面的一个示例,首先在选择之外进行克隆,然后删除该选项,当您需要附加外部克隆选项时
$("#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");
希望这有帮助!!!
关于javascript - 我想通过 optgroup 下拉列表添加和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46069552/