jQuery獲取及設置單選框、多選框、文本框

jQuery獲取及設置單選框、多選框、文本框

獲取一組radio被選中項的值
var item = $("input[@name=items][@checked]").val();
獲取select被選中項的文本
var item = $("select[@name=items] option[@selected]").text();
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();

控制表單元素:

文本框,文本區域:$("#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的項目爲當前選中項 (

$("input[@name=radio_s][@value=16]").attr("checked",true);(測試通過

下拉框select:

$("#sel").attr("value","-sel3");//設置value=-sel3的項目爲當前選中項
$("11112222").appendTo("#sel")//添加下拉框的option
$("#sel").empty();//清空下拉框

$("#select1")[0].options(index).selected = true; //使第index個option選中
$("#select1")[0].options(3).text //取索引爲3的option值

 
 
===================================另一篇文章==================================
$(document).ready(function() {  
    $("#btnSubmit").click(function() {  
        alert($("input[name='RadioButtonList1']:checked").val());  
    });  
});  
 
<asp:RadioButtonList ID="RadioButtonList1" runat="server">  
    <asp:ListItem>1</asp:ListItem>  
    <asp:ListItem>2</asp:ListItem>  
    <asp:ListItem>3</asp:ListItem>  
</asp:RadioButtonList>  
  
<input id="btnSubmit" type="button" value="Submit" /> 
發佈了12 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章