jquery 获取表单值

alert($("#hid1").attr("value")); 获取隐藏控件的值
alert($("#lbl1").attr("innerText")); 获取lable控件的值

alert($("#txt1").attr("value")); 获取textbox控件的值 方法1

alert($("#txt1").val()); 获取textbox控件的值 方法2

 

 

/*获取单选按钮的值*/

   var valradio = $("input[@type=radio][@checked]").val();

/*获取复选框的值*/

var checkboxval = $("#checkbox_id").attr("value")

/*获取下拉列表中所有的值*/

   var selectval = $('#select_id').val();

//获取下拉列表选取中的值,此方法针对所有下拉框都起作用的

//此方法针对所有下拉框都起作用的

   //如果针对某ID进行获取,$(‘#id>option’).each()即可

 

 $('select>option').each(function(){

    if($(this).attr('selected')==true)

    {

    alert($(this).text());

    }

  })

}

3.另外对表单的其他处理:

//控制表单元素:

//文本框,文本区域:

$("#text_id").attr("value",'');//清空内容

$("#text_id").attr("value",'test');//填充内容

//多选框checkbox

$("#chk_id").attr("checked",'');//未选中的值

$("#chk_id").attr("checked",true);//选中的值

if($("#chk_id").attr('checked')==undefined) //判断是否已经选中

//单选组radio

$("input[@type=radio]").attr("checked",'10');//设置value=10的单选按钮为当前选中项

//下拉框select

$("#select_id").attr("value",'test');//设置value=test的项目为当前选中项

$("<option value='test'>test</option><option value='test2'>test2</option>").appendTo("#select_id")//添加下拉框的option

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

 

原文地址:https://www.cnblogs.com/hyd309/p/1517859.html