1. select元素
1.1获取select选中的value和text值
<pre name="code" class="html"><select id="select1">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>$("#select1 option:selected").val();
or
$("select1").find("option:selected").val();
$("select1 option:selected").text();
or
$("select1").find("option:selected").text();1.2.为select设置选中
设置option value为2的项选中
var s='2';
$("#select1").val(s); //IE8 Firefox29有效设置option text为C的项选中
$("#select1").find("option[text='C']").attr("selected",true); //IE Firefox测试无效
2.checkbox元素
<input type="checkbox" id="chk1"/> 选项
2.1 checkbox检查是否勾选
if($("#chk1").attr("checked")=='checked'){
alert("checked");
}else{
alert("unchecked");
}
2.2 设置checkbox选中
$("#chk1").attr("checked",true);
2.3 设置不选中
$("#chk1").removeAttr("checked");
2.4 设置checkbox不可编辑
<input type="checkbox" id="chk1" disabled = "disabled"/>or
$("#chk1").attr("disabled","disabled");
去除不可编辑
$("#chk1").removeAttr("disabled");
本文详细介绍了如何使用HTML和JavaScript操作select元素的值和文本,以及如何为select设置选中状态,同时展示了如何检查和设置checkbox的选中状态,并提供了相关代码示例。
1681

被折叠的 条评论
为什么被折叠?



