JQUERY获取form表单值

来源:互联网 发布:怎么做淘宝店招 编辑:程序博客网 时间:2024/04/19 17:26
jquery如何取得text,areatext,radio,checkbox,select的值,以及其他一些操作;假如我们有如下页面:

 <input type="text" name="textname" id="text_id" value="">

   ...........在此不写出来了

下面来看怎么取得FORM中的各种值等等;

function get_form_value(){

/*获得TEXT.AREATEXT的值*/

   var textval = $("#text_id").attr("value");//或者

   var textval = $("#text_id").val();

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

   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();//清空下拉框  

原创粉丝点击