jquery获取和操作表单元素输入框 下拉框 单选 复选框

checkbox 多选框的操作。
1、可以通过name选择器选中checkbox进行遍历,在点击时候增加方法 :
$("input[name='checkboxname']").each(function(){
//遍历时候增加方法
$(this).click(function(){
var checked = $(this).is('checked'); //判断当前是否是选中状态。 返回 true false
var checkval = $(this).val();
});
});
2、设置checkbox 选择中状态
$("#checkid").prop("checked",true);
3、 设置checkbox不可勾选,不可编辑
$("#checkid").attr('disabled',true);
//获得选中的checked的长度
$("input:radio[name='checkboxname1']:checked").length;
4、jquery 设置页面元素不可点击、不可编辑、只读(备忘)
$("input").attr('readonly', true); //输入框只读
$("textarea").attr('readonly', true); //textarea只读
$(':radio').attr('disabled', true); // radio 只读不可选
$(':checkbox').attr('disabled', true);
$(':button').attr('disabled', true); //按钮不可点击
$('a').removeAttr('onclick'); //a标签去掉点击事件
$('select').attr('disabled', true); //下拉框不可选

5、通过获取radio的值, 判断是否选择了 radio
$("input[type='radio']").click(function(){
var inputval=$("input:radio[name='radioname1']:checked").val();
if(inputval==undefined){ alert("请选择radio");}
if(inputval!=""){
alert(inputval);//name为radioname1的radio 没有选择
}
})

//选择的radio的值。
  $("input:radio[name='jinbei']:checked").val();
//给选中的增加属性
  $("input:radio[name='jinbei']:checked").attr("class");

//获得 radio 是否是选中状态
var radioischecked = $("#radioid").id(":checked"); //返回 true 或者false;
5.1设置radio 选中默认值

启用 停用

设置intput中 name=status的value=0的被选中 $("("input:[name='status'][value='0']")").attr('checked',true);

$(".font").find("input[name='status']").removeAttr("checked");
if ('1' == result.status){
$("#open").attr("checked","checked");
} else if ('0' == result.status){
$("#close").attr("checked","checked");
}
5.2 val 为指定的值: $(":radio[name='drawStatus'][value='" + val + "']").prop("checked", "checked")

6、设置 下拉框 指定值被选中
$("#selectid option:first").prop("selected","selected");

7、关于select 下拉框的更多操作:获取Select :

获取select 选中的 text :

$("#ddlRegType").find("option:selected").text();

设置select的value为0的为选中状态。
$("#selectid").val("0");

获取select选中的 value:
$("#ddlRegType").val();
获取select选中的索引:
$("#ddlRegType ").get(0).selectedIndex;
设置select:
设置select 选中的索引:
$("#ddlRegType ").get(0).selectedIndex=index;//index为索引值
设置select 选中的value:
$("#ddlRegType ").attr("value","Normal“);

$("#ddlRegType ").val("Normal");

$("#ddlRegType ").get(0).value = value;

设置select 选中的text:

var count=$("#ddlRegType option").length;
for(var i=0;i<count;i++)
{ if($("#ddlRegType ").get(0).options[i].text == text)
{
$("#ddlRegType ").get(0).options[i].selected = true;
break;
}
}
$("#select_id option[text='jQuery']").attr("selected", true);
设置select option项:
$("#select_id").append(""); //添加一项option

$("#select_id").prepend(""); //在前面插入一项option

$("#select_id option:last").remove(); //删除索引值最大的Option

$("#select_id option[index='0']").remove();//删除索引值为0的Option

$("#select_id option[value='3']").remove(); //删除值为3的Option

$("#select_id option[text='4']").remove(); //删除TEXT值为4的Option

清空 Select:

$("#ddlRegType ").empty();

原文地址:https://www.cnblogs.com/sunny3158/p/14141053.html