`

JQUERY获取form表单值

阅读更多

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics