javascript 关于select、checkbox状态、值、等的事情

/**
* select中options的操作
*/
//获取select下面所有option
$("#select").find("option");

//选中select下面值为value1 的option
$("#select").val("value1");

//设置它为默认选中
$("#select").val("value1").attr("selected",true);

//select的级联,即第二个select的值随着第一个select选中的值变化。
$(".selector1").change(function(){
// 先清空第二个
$(".selector2").empty();
// 实际的应用中,这里的option一般都是用循环生成多个了
//option 可以使用 new Option() 生成
var option = $("<option>").val(1).text("pxx");
$(".selector2").append(option);
});



/**
* checkbox
*/
//checkbox设置不能选择和取消不能选择
$("#selector").attr("disabled","disabled");
$("#selector").removeAttr("disabled");

//检查checkbox是否为选中状态
$('#selector').is(':checked');

//checkbox 设置为选中状态
$('#selector').attr("checked","true");

//checkbox绑定change事件
$("#selector").change(function() {
alert("checked");
});

$("#selector").on("change",function () {
alert("checked");
});

//checkbox绑定click事件
$('input:checkbox').click(function () {
this.blur();
this.focus();
});
$("#selector").on("click",function () {
this.blur();
this.focus();
});

猜你喜欢

转载自www.cnblogs.com/doing-good/p/10812143.html