溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

各種選擇框jQuery的選中方法是什么

發(fā)布時(shí)間:2021-08-11 15:24:12 來(lái)源:億速云 閱讀:146 作者:小新 欄目:web開(kāi)發(fā)

這篇文章主要介紹各種選擇框jQuery的選中方法是什么,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

select下拉列表的選中方法是:$("slect option:eq(1)").attr("selected",true);//選中第二個(gè)option

chekbox的選中方法:$("[value=check1"]:checkbox).attr("checked",true);

radio的選中方法:$("[value=radio2"]:radio).attr("checked",true);

以上是簡(jiǎn)寫(xiě),重要的是看代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>3-10-4</title>
<style type="text/css">
.test{
 font-weight:bold;
 color : red;
}
.add{
 font-style:italic;
}
</style>
 <!--  引入jQuery -->
 <script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
 <script type="text/javascript">
 //<![CDATA[
 $(function(){
   //設(shè)置單選下拉框選中
   $("input:eq(0)").click(function(){
      $("#single option").removeAttr("selected"); //移除屬性selected
      $("#single option:eq(1)").attr("selected",true); //設(shè)置屬性selected
   });
   //設(shè)置多選下拉框選中
   $("input:eq(1)").click(function(){
      $("#multiple option").removeAttr("selected"); //移除屬性selected
      $("#multiple option:eq(2)").attr("selected",true);//設(shè)置屬性selected
      $("#multiple option:eq(3)").attr("selected",true);//設(shè)置屬性selected
   });
   //設(shè)置單選框和多選框選中
   $("input:eq(2)").click(function(){
      $(":checkbox").removeAttr("checked"); //移除屬性checked
      $(":radio").removeAttr("checked"); //移除屬性checked
      $("[value=check2]:checkbox").attr("checked",true);//設(shè)置屬性checked
      $("[value=check3]:checkbox").attr("checked",true);//設(shè)置屬性checked
      $("[value=radio2]:radio").attr("checked",true);//設(shè)置屬性checked
   });
 });
 //]]>
 </script>
</head>
<body>
  <input type="button" value="設(shè)置單選下拉框選中"/>
  <input type="button" value="設(shè)置多選下拉框選中"/>
  <input type="button" value="設(shè)置單選框和多選框選中"/>

<br/><br/>

<select id="single">
 <option>選擇1號(hào)</option>
 <option>選擇2號(hào)</option>
 <option>選擇3號(hào)</option>
</select>

<select id="multiple" multiple="multiple" >
 <option selected="selected">選擇1號(hào)</option>
 <option>選擇2號(hào)</option>
 <option>選擇3號(hào)</option>
 <option>選擇4號(hào)</option>
 <option selected="selected">選擇5號(hào)</option>
</select>

<br/><br/>


<input type="checkbox" value="check1"/> 多選1
<input type="checkbox" value="check2"/> 多選2
<input type="checkbox" value="check3"/> 多選3
<input type="checkbox" value="check4"/> 多選4

<br/>

<input type="radio" value="radio1" name="a"/> 單選1
<input type="radio" value="radio2" name="a"/> 單選2
<input type="radio" value="radio3" name="a"/> 單選3
</body>
</html>

以上是“各種選擇框jQuery的選中方法是什么”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI