假如我们有如下页面:
...........在此不写出来了
下面来看怎么取得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的项目为当前选中项
$(testtest2).appendto(#select_id)//添加下拉框的option
$(#select_id).empty();//清空下拉框