我建议:
$('#shipping_customer_address option:last').prop('selected', true);
$('#shipping_customer_address option:last').prop('selected', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="shipping_address_id" id="shipping_customer_address" class="customer_address" title="" onchange="shipping.newAddress(!this.value)">
<option value="48" selected="selected">p p, yui, cfg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="52" selected="selected">e B, ewri, csdfwefg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="">New Address</option>
</select>
外部JS Fiddle 演示。
或者(快一点):
$('#shipping_customer_address option').last().prop('selected',true);
$('#shipping_customer_address option').last().prop('selected',true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="shipping_address_id" id="shipping_customer_address" class="customer_address" title="" onchange="shipping.newAddress(!this.value)">
<option value="48" selected="selected">p p, yui, cfg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="52" selected="selected">e B, ewri, csdfwefg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="">New Address</option>
</select>
外部JS Fiddle 演示。
您可以简单地value
从选项中删除该属性New Address
并通过缺少该属性进行选择:
$('#shipping_customer_address option').not('[value]').prop('selected',true);
$('#shipping_customer_address option').not('[value]').prop('selected',true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="shipping_address_id" id="shipping_customer_address" class="customer_address" title="" onchange="shipping.newAddress(!this.value)">
<option value="48" selected="selected">p p, yui, cfg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="52" selected="selected">e B, ewri, csdfwefg, 90602-1234, U.S. Minor Outlying Islands</option>
<option>New Address</option>
</select>
外部JS Fiddle 演示。
甚至可以通过空属性选择:
$('#shipping_customer_address option[value=""]').prop('selected',true);
$('#shipping_customer_address option[value=""]').prop('selected',true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="shipping_address_id" id="shipping_customer_address" class="customer_address" title="" onchange="shipping.newAddress(!this.value)">
<option value="48" selected="selected">p p, yui, cfg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="52" selected="selected">e B, ewri, csdfwefg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="">New Address</option>
</select>
外部JS Fiddle 演示。
或者,在纯 JavaScript 中,选择最后一个<option>
:
document.getElementById('shipping_customer_address').lastElementChild.selected = true;
document.getElementById('shipping_customer_address').lastElementChild.selected = true;
<select name="shipping_address_id" id="shipping_customer_address" class="customer_address" title="" onchange="shipping.newAddress(!this.value)">
<option value="48" selected="selected">p p, yui, cfg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="52" selected="selected">e B, ewri, csdfwefg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="">New Address</option>
</select>
外部JS Fiddle 演示。
或者,<option>
使用 CSS 选择器选择最后一个document.querySelector()
:
document.querySelector('#shipping_customer_address option:last-child').selected = true;
document.querySelector('#shipping_customer_address option:last-child').selected = true;
<select name="shipping_address_id" id="shipping_customer_address" class="customer_address" title="" onchange="shipping.newAddress(!this.value)">
<option value="48" selected="selected">p p, yui, cfg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="52" selected="selected">e B, ewri, csdfwefg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="">New Address</option>
</select>
外部JS Fiddle 演示。
或者,对于那些无法识别的浏览器lastElementChild
:
var options = document.getElementById('shipping_customer_address').options;
options[options.length - 1].selected = true;
var options = document.getElementById('shipping_customer_address').options;
options[options.length - 1].selected = true;
<select name="shipping_address_id" id="shipping_customer_address" class="customer_address" title="" onchange="shipping.newAddress(!this.value)">
<option value="48" selected="selected">p p, yui, cfg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="52" selected="selected">e B, ewri, csdfwefg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="">New Address</option>
</select>
外部JS Fiddle 演示。
要选择具有空value
属性/属性的选项,请使用document.querySelector()
:
document.querySelector('#shipping_customer_address option[value=""]').selected = true;
document.querySelector('#shipping_customer_address option[value=""]').selected = true;
<select name="shipping_address_id" id="shipping_customer_address" class="customer_address" title="" onchange="shipping.newAddress(!this.value)">
<option value="48" selected="selected">p p, yui, cfg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="52" selected="selected">e B, ewri, csdfwefg, 90602-1234, U.S. Minor Outlying Islands</option>
<option value="">New Address</option>
</select>
外部JS Fiddle 演示。
参考: