您现在的位置是:首页 > 其他

李清波 2015-09-20 其他 337381 复制当前网址

jQuery操作表单值的方法大全

下面是本人整理的一些jquery获取各种类型表单值得方法。



常用的表单类型有:

text,areatext,radio,checkbox,select


现在我们学习以下基本获取赋值的,以及其他一些操作,废话少说,直接看示例吧。



获取文本框中的值

<input type="text" value="some text"/>

如果我们想获取里面value的值

$("input").val();
//获得 some text

但如果这个页面有很多input我们就不能这样获取了,但我们可以根据id和class来获取


如:

//获取表单input 里面,ID为:text_id的值 var textval = $("#text_id").val();
 var textval = $("#text_class").val();

以上这两种结果都一样,只不过获取的方式不一样而已



//多选框 checkbox:

    $("#chk_id").attr("checked",'');//未 选中的值
    $("#chk_id").attr("checked",true);//选 中的值
    if($("#chk_id").attr('checked')==undefined) //判断是否已经选中


//单选组radio:

   

 //获取表单radion的值有以下几种
 $('input[name="testradio"]:checked').val();
 $('input:radio:checked').val();
 $('input[name="testradio"]:eq(1)').val()
 
 
 //这样就是第一个选中咯。
 $("input[name=jizai]:eq(0)").attr("checked",'checked'); 

 //移除选中按钮
 $("#rdo1").removeAttr("checked");

 

//下拉框select:

   

$("#select_id").attr("value",'test');//设置value=test的项目为当前选中项
$(".selector").find("option[text='pxx']").attr("selected",true);    //设置text为pxx的项选中
 
 
$("testtest2").appendTo("#select_id")//添加下拉框的option


$("#select_id").empty();//清空下拉框


$(".selector").val();    //获取当前选中项的value
$(".selector").find("option:selected").text();    //获取当前选中项的text


遍历Radio

$('input:radio').each(function(index,domEle){
     //写入代码
});


文章来源:http://liqingbo.com/blog-85.html

评论