jquery块<;选择>;第一选择
我有这样一个代码:jquery块<;选择>;第一选择,jquery,select,Jquery,Select,我有这样一个代码: <select name="aa"> <option>a</option> <option>b</option> <option>c</option> </select> $("select[name='aa'] option:gt(0)").remove(); $("select[name='aa'] option:gt(0)").attr("di
<select name="aa">
<option>a</option>
<option>b</option>
<option>c</option>
</select>
$("select[name='aa'] option:gt(0)").remove();
$("select[name='aa'] option:gt(0)").attr("disabled", "disabled");
$("select[name='aa'] option:eq(0)").attr("selected", "selected");
A.
B
C
有一种方法可以停止打开所有“我的表单”的“选择”,并在本例中使“仅显示第一个选择”成为“仅显示第一个选择”
提前谢谢
ciao h使用
:gt(0)
过滤器删除所有其他选项(0)
是如下索引:
<select name="aa">
<option>a</option>
<option>b</option>
<option>c</option>
</select>
$("select[name='aa'] option:gt(0)").remove();
$("select[name='aa'] option:gt(0)").attr("disabled", "disabled");
$("select[name='aa'] option:eq(0)").attr("selected", "selected");
或者像这样禁用它们:
<select name="aa">
<option>a</option>
<option>b</option>
<option>c</option>
</select>
$("select[name='aa'] option:gt(0)").remove();
$("select[name='aa'] option:gt(0)").attr("disabled", "disabled");
$("select[name='aa'] option:eq(0)").attr("selected", "selected");
然后用:eq(0)
过滤器选择一个项目(0)
是这样的索引:
<select name="aa">
<option>a</option>
<option>b</option>
<option>c</option>
</select>
$("select[name='aa'] option:gt(0)").remove();
$("select[name='aa'] option:gt(0)").attr("disabled", "disabled");
$("select[name='aa'] option:eq(0)").attr("selected", "selected");
可使用:not
过滤器进行反向操作:
$("select[name='aa'] :not(option:gt(0))").attr("disabled", "disabled");
$("select[name='aa'] option:lt(1)").attr("disabled", "disabled");
或:lt
过滤器:
$("select[name='aa'] :not(option:gt(0))").attr("disabled", "disabled");
$("select[name='aa'] option:lt(1)").attr("disabled", "disabled");
只需分配一个空字符串.attr(“disabled”,”)
或.attr(“选定的”、“已选择的”)
以删除属性设置
// hide items
var src = $("select[name='aa'] option:eq(0)");
var tar = $("select[name='ab']").hide();
tar.append(src.clone())
src.remove();
//reshow items:
src = $("select[name='ab'] option:eq(0)");
tar = $("select[name='aa']")
tar.prepend(src.clone())
src.remove();
要切换/触发值,我认为您需要第二次(隐藏)选择:
// hide items
var src = $("select[name='aa'] option:eq(0)");
var tar = $("select[name='ab']").hide();
tar.append(src.clone())
src.remove();
//reshow items:
src = $("select[name='ab'] option:eq(0)");
tar = $("select[name='aa']")
tar.prepend(src.clone())
src.remove();
什么?你是说选择默认选项?尝试在a选项上使用选定的属性
<option selected>a</option>
a
或使选项不可选择:
<option disabled>a</option>
a
您可以执行以下操作:
$("select[name='aa']").val($("select[name='aa'] option:first").val());
您可以禁用您不希望被选中的内容,或者更改其css显示,如:
$('select[name="aa"] option').not(':first').css('display','none');
如果要自动设置“默认”值,并且不允许任何人更改它,可以设置第一个值,然后禁用整个选择元素,这样就不会有人更改它:
$('select[name="aa"]')
// set the value of the first option
.val($('select[name="aa"] option:first').val())
// disable the select element so that no one can alter it's value
.attr('disabled','disabled');
不,我需要一个不同的东西,我需要阻止显示b c的可能性,谢谢你的回答。@haltman也许你可以使用hide()和show()来代替remove(),IIRC一些浏览器(IE?)不支持隐藏/显示
选项
s.@RoToRa,的确如此;)这就是为什么你必须移除它们,而不是隐藏。