登录 注册

jquery表单元素取值和赋值

收藏
[Javascript] 标签:jquery 2012-05-08 15:06
对select表单的操作,另见:#14
原始代码 全屏查看 0评 / 0藏 / 5541阅  跳至 / 57行
// input表单取值
val = $("#id")[0].value;
val = $("#id").val();

// 赋值
$("#id")[0].value =  "new value";
$("#id").val("new value");

/*获得TEXT.AREATEXT的值*/
var textval = $("#text_id").attr("value");
var textval = $("#text_id").val();
 
/*获取单选按钮的值*/
var valradio = $("input[type=radio]:checked").val();
 
/*获取一组名为(items)的radio被选中项的值*/
var item = $('input[name=items]:checked').val();
 
/*获取复选框的值*/
var checkboxval = $("#checkbox_id").attr("value");
 
/*获取下拉列表的值*/
var selectval = $('#select_id').val();
 
/*文本框,文本区域*/
$("#text_id").attr("value","");//清空内容
$("#text_id").attr("value","test");//填充内容
 
/*多选框checkbox*/
$("#chk_id").attr("checked","");//使其未勾选
$("#chk_id").attr("checked",true);//勾选
if($("#chk_id").attr('checked')==true) //判断是否已经选中
 
/*单选组radio*/
$("input[type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项
 
/*下拉框select*/
$("#select_id").attr("value",'test');//设置value=test的项目为当前选中项
$("testtest2").appendTo("#select_id")//添加下拉框的option
$("#select_id").empty();//清空下拉框
 
/*获取一组名为(items)的radio被选中项的值*/
var item = $('input[name=items]:checked').val(); //若未被选中 则val() = undefined
 
/*获取select被选中项的文本*/
var item = $("select[name=items] option:selected").text();
 
/*select下拉框的第二个元素为当前选中值*/
$('#select_id')[0].selectedIndex = 1;
 
/*radio单选组的第二个元素为当前选中值*/
$('input[name=items]').get(1).checked = true;
 
/*重置表单*/
$("form").each(function(){
.reset();
});

最新评论

  · · · · · ·  (共0条)

目前还没有评论

登录后您才可以发表评论。 马上登录 立即注册
cottage
2012-02-29加入
一段精湛的代码,天涯何处寻知音!
Back to Top