日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

jQuery对表单元素的取值和赋值操作代码

發(fā)布時(shí)間:2025/7/14 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jQuery对表单元素的取值和赋值操作代码 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用常規(guī)的思路:$(“#keyword”).value 取值是取不到的,因?yàn)榇藭r(shí)$(‘#keydord’)已經(jīng)不是個(gè)element,而是個(gè)jquery對(duì)象,所以應(yīng)該使用:$(“#keyword”).val() $("#keyword")[0].value = ""; /*獲得TEXT.AREATEXT的值*/ var textval = $("#text_id").attr("value"); //或者 var textval = $("#text_id").val(); /*獲取單選按鈕的值*/ var valradio = $("input[type=radio]:checked").val(); /*獲取一組名為(items)的radio被選中項(xiàng)的值*/ var item = $('input[name=items]:checked').val(); /*獲取復(fù)選框的值*/ var checkboxval = $("#checkbox_id").attr("value"); /*獲取下拉列表的值*/ var selectval = $('#select_id').val(); /*文本框,文本區(qū)域*/ $("#text_id").attr("value",");//清空內(nèi)容 $("#text_id").attr("value",'test');//填充內(nèi)容 /*多選框checkbox*/ $("#chk_id").attr("checked",");//使其未勾選 $("#chk_id").attr("checked",true);//勾選 if($("#chk_id").attr('checked')==true) //判斷是否已經(jīng)選中 /*單選組radio*/ $("input[type=radio]").attr("checked",'2');//設(shè)置value=2的項(xiàng)目為當(dāng)前選中項(xiàng) /*下拉框select*/ $("#select_id").attr("value",'test');//設(shè)置value=test的項(xiàng)目為當(dāng)前選中項(xiàng) $("testtest2").appendTo("#select_id")//添加下拉框的option $("#select_id").empty();//清空下拉框 /*獲取一組名為(items)的radio被選中項(xiàng)的值*/ var item = $('input[name=items]:checked').val(); //若未被選中 則val() = undefined /*獲取select被選中項(xiàng)的文本*/ var item = $("select[name=items] option:selected").text(); /*select下拉框的第二個(gè)元素為當(dāng)前選中值*/ $('#select_id')[0].selectedIndex = 1; /*radio單選組的第二個(gè)元素為當(dāng)前選中值*/ $('input[name=items]').get(1).checked = true; /*重置表單*/ $("form").each(function(){ .reset(); }); jQuery對(duì)html元素取值與賦值 Jquery給基本控件的取值、賦值 TEXTBOX: var str = $('#txt').val(); $('#txt').val("Set Lbl Value"); //文本框,文本區(qū)域: $("#text_id").attr("value",'');//清空內(nèi)容 $("#text_id").attr("value",'test');// 填充內(nèi)容 Jquery給基本控件的取值、賦值 TEXTBOX: var str = $('#txt').val(); $('#txt').val("Set Lbl Value"); //文本框,文本區(qū)域: $("#text_id").attr("value",'');//清空內(nèi)容 $("#text_id").attr("value",'test');// 填充內(nèi)容 LABLE: var str = $('#lbl').text(); $('#lbl').text("Set Lbl Value"); var valradio = $("input[@type=radio][@checked]").val(); var item = $('input[@name=items][@checked]').val(); var checkboxval = $("#checkbox_id").attr("value"); var selectval = $('#select_id').val(); //多選框checkbox: $("#chk_id").attr("checked",'');//使其未勾選 $("#chk_id").attr("checked",true);// 勾選 if($("#chk_id").attr('checked')==true) //判斷是否已經(jīng)選中 單選組radio: $("input[@type=radio]").attr("checked",'2'); //設(shè)置value=2的項(xiàng)目為當(dāng)前選中項(xiàng) //下拉框select: $("#select_id").attr("value",'test');// 設(shè)置value=test的項(xiàng)目為當(dāng)前選中項(xiàng) $("<option value='test'>test</option><option value='test2'>test2</option>").appendTo("#select_id")//添加下拉框的 option $("#select_id").empty();//清空下拉框 獲取一組名為 (items)的radio被選中項(xiàng)的值 var item = $('input[@name=items][@checked]').val();//若未被選中 則val() = undefined 獲 取select被選中項(xiàng)的文本 var item = $("select[@name=items] option[@selected]").text(); select下拉框的第二個(gè)元素為當(dāng)前選中值 $('#select_id')[0].selectedIndex = 1; radio單選組的第二個(gè)元素為當(dāng)前選中值 $('input[@name=items]').get(1).checked = true; //重置表單 $("form").each(function(){ .reset(); }); 摘要: Jquery給基本控件的取值、賦值 TEXTBOX: var str = $('#txt').val(); $('#txt').val("Set Lbl Value"); //文本框,文本區(qū)域: $("#text_id").attr("value",'');//清空內(nèi)容 $("#text_id").attr("value",'test');// 填充內(nèi)容 Jquery給基本控件的取值、賦值 TEXTBOX: var str = $('#txt').val(); $('#txt').val("Set Lbl Value"); //文本框,文本區(qū)域: $("#text_id").attr("value",'');//清空內(nèi)容 $("#text_id").attr("value",'test');// 填充內(nèi)容 LABLE: var str = $('#lbl').text(); $('#lbl').text("Set Lbl Value"); var valradio = $("input[@type=radio][@checked]").val(); var item = $('input[@name=items][@checked]').val(); var checkboxval = $("#checkbox_id").attr("value"); var selectval = $('#select_id').val(); //多選框checkbox: $("#chk_id").attr("checked",'');//使其未勾選 $("#chk_id").attr("checked",true);// 勾選 if($("#chk_id").attr('checked')==true) //判斷是否已經(jīng)選中 單選組radio: $("input[@type=radio]").attr("checked",'2'); //設(shè)置value=2的項(xiàng)目為當(dāng)前選中項(xiàng) //下拉框select: $("#select_id").attr("value",'test');// 設(shè)置value=test的項(xiàng)目為當(dāng)前選中項(xiàng) $("<option value='test'>test</option><option value='test2'>test2</option>").appendTo("#select_id")//添加下拉框的 option $("#select_id").empty();//清空下拉框 獲取一組名為 (items)的radio被選中項(xiàng)的值 var item = $('input[@name=items][@checked]').val();//若未被選中 則val() = undefined 獲 取select被選中項(xiàng)的文本 var item = $("select[@name=items] option[@selected]").text(); select下拉框的第二個(gè)元素為當(dāng)前選中值 $('#select_id')[0].selectedIndex = 1; radio單選組的第二個(gè)元素為當(dāng)前選中值 $('input[@name=items]').get(1).checked = true; //重置表單 $("form").each(function(){ .reset(); });

?

總結(jié)

以上是生活随笔為你收集整理的jQuery对表单元素的取值和赋值操作代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。