ruoyiqing 2014-03-18
jquery取radio单选按钮的值
$("input[name='items']:checked").val();
另:判断radio是否选中并取得选中的值
jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关
获取一组radio或者checkbox被选中项的值
var template=new Array();
$('input[name="template"]:checked').each(function(){
template.push($(this).val());//向数组中添加元素
});
获取select被选中项的文本
var item = $("select[name=items] option[selected]").text();
获取select被选中项的值
var item = $("select[name=items] option[selected]").val();
$('#testSelect option:selected').text();//选中的文本
$('#testSelect option:selected') .val();//选中的值
select下拉框的第二个元素为当前选中值
$('#select_id')[0].selectedIndex = 1;
radio单选组的第二个元素为当前选中值
$('input[name=items]').get(1).checked = true;
获取值:
文本框,文本区域:$("#txt").attr("value");
多选框checkbox:$("#checkbox_id").attr("value");
单选组radio: $("input[type=radio][checked]").val();
下拉框select: $('#sel').val();或$('#sel option:selected').val();
控制表单元素:
文本框,文本区域:$("#txt").attr("value",'');//清空内容
$("#txt").attr("value",'11');//填充内容
多选框checkbox: $("#chk1").attr("checked",'');//不打勾
$("#chk2").attr("checked",true);//打勾
if($("#chk1").attr('checked')==undefined) //判断是否已经打勾
单选组radio: $("input[type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项
下拉框select: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项
$("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//添加下拉框的option
$("#sel").empty();//清空下拉框
val() :获得第一个匹配元素的当前值。
val(val):设置每一个匹配元素的值。
所以,代码应该这样写:
取值:val = $("#id")[0].value;
赋值: $("#id")[0].value = "new value";
或者$("#id").val("new value");
jQuery中each非常好用,常用它取代javascript的for循环
在each代码块内不能使用break和continue,要实现break和continue的功能的话,要使用其它的方式
break----用return false;
continue --用return ture;
所以当我在each里想使用return true给这个function返回时,其实只是让each继续执行而以
连each都没有中断,所以function也就不能return了
另:判断radio是否选中并取得选中的值
如下所示:
function checkradio(){
var item = $(":radio:checked");
var len=item.length;
if(len>0){
alert("yes--选中的值为:"+$(":radio:checked").val());
}
}