
var text1 = 'Two';$("select option").filter(function() { //may want to use $.trim in here return $(this).text() == text1;}).prop('selected', true);<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><select> <option value="0">One</option> <option value="1">Two</option></select>低于1.6且大于等于1.4的jQuery版本var text1 = 'Two';$("select option").filter(function() { //may want to use $.trim in here return $(this).text() == text1;}).attr('selected', true);<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script><select> <option value="0">One</option> <option value="1">Two</option></select>请注意,虽然此方法在高于1.6但低于1.9的版本中可以使用,但自1.6开始不推荐使用。这将无法正常工作在jQuery的1.9+。
之前的版本
val()应该处理两种情况。
$('select').val('1'); // selects "Two"$('select').val('Two'); // also selects "Two"<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script><select> <option value="0">One</option> <option value="1">Two</option></select>欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)