jQuery对html元素取值与赋值
以下總結(jié)了常用的jQuery選擇器對html元素取值與賦值
Textbox:?
var str = $('#txt').val();
$('#txt').val("Set Lbl Value");?
文本框,文本區(qū)域:
$("#text_id").attr("value",'');//清空內(nèi)容
$("#text_id").attr("value",'test');// 填充內(nèi)容
Label:???
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();
?});
轉(zhuǎn)載于:https://www.cnblogs.com/GISQZC/p/5225362.html
總結(jié)
以上是生活随笔為你收集整理的jQuery对html元素取值与赋值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有关怎么在不创建新的按钮的前提下改变返回
- 下一篇: 移动測试技术保护源码!解码全球首款移动端