国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

jquery對(duì)單選框,多選框,文本框等常見操作小結(jié)

2019-11-20 21:17:34
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一、文本框、單選按鈕、復(fù)選框、相關(guān)操作

復(fù)制代碼 代碼如下:

var sex=$("input[name='sex']:checked").val();   //獲取一組radio被選中項(xiàng)的值 
var item=$("#sel option:selected").text();      //獲取select被選中項(xiàng)的文本 
var option_num=$('#sel').val();                 //獲取select項(xiàng)目索引
$("#sel")[0].selectedIndex =1;                  //select下拉框的第二個(gè)元素為當(dāng)前選中值
$("input[name='sex']").get(1).checked=true;     //radio單選組的第二個(gè)元素為當(dāng)前選中值

 或者對(duì)單選框默認(rèn)選定設(shè)置:
$("input[name='sex']").each(function(){
            if($(this).val()==s){
                $(this).attr("checked",true);
                                //this.checked=true;
            }
        });


Jquery 根據(jù)value值設(shè)置下拉列表(select)默認(rèn)選中項(xiàng)
復(fù)制代碼 代碼如下:

  <select name=sel onchange="bao(this.options[this.options.selectedIndex].value)">
  <option value="">請(qǐng)選擇
  <option value="1">Item 1
  <option value="2">Item 2
  <option value="3">Item 3
  </select>
  <script>
  function bao(s)
  {
     txt.value=s;
     //選擇后,讓第一項(xiàng)被選中,這樣,就有Change啦.
     document.all.sel.options[0].selected=true;
 }
 </script>
 <textarea id=txt></textarea>

二、 jQuery獲取 Select選擇的Text和Value
復(fù)制代碼 代碼如下:

語(yǔ)法解釋:
 $("#select_id").change(function(){//code...});   //為Select添加事件,當(dāng)選擇其中一項(xiàng) 時(shí)觸發(fā)
 var checkText=$("#select_id").find("option:selected").text();  //獲取Select選擇的Text
 var checkValue=$("#select_id").val();  //獲取Select選擇的Value
 var checkIndex=$("#select_id ").get(0).selectedIndex;  //獲取Select選擇的索引值
 var maxIndex=$("#select_id option:last").attr("index");  //獲取Select最大的索引值

jQuery設(shè)置Select選擇的Text和Value:
語(yǔ)法解釋:
 $("#select_id ").get(0).selectedIndex=1;  //設(shè)置Select索引值為1的項(xiàng)選中
 $("#select_id ").val(4);   //設(shè)置Select的Value值為4的項(xiàng)選中
 $("#select_id option[text='jQuery']").attr("selected", true);   //設(shè)置 Select的Text值為jQuery的項(xiàng)選中

jQuery添加/刪除Select的Option項(xiàng):
語(yǔ)法解釋:
 $("#select_id").append("<option value='Value'>Text</option>");  //為Select追加一個(gè)Option(下拉項(xiàng))
 $("#select_id").prepend("<option value='0'>請(qǐng)選擇</option>");  //為Select插入一個(gè)Option(第一個(gè)位置)
 $("#select_id option:last").remove();  //刪除Select中索引值最大Option(最后一個(gè))
 $("#select_id option[index='0']").remove();  //刪除Select中索引值為0的 Option(第一個(gè))
 $("#select_id option[value='3']").remove();  //刪除Select中Value='3'的 Option
 $("#select_id option[text='4']").remove();  //刪除Select中Text='4'的Option


應(yīng)用:
復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>jQuery common</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        //初始化下拉列表--動(dòng)態(tài)添加
        var item = ['幼兒園','小學(xué)','初中','高中','大學(xué)','研究生','博士','碩士'];
        var html ="<option value='0'>請(qǐng)選擇</option>";
        for (var i = 0;i < item.length;i++){
            html += "<option value='"+(i+1)+"'>"+item[i]+"</option>";
        }
        $("#grade").empty().append(html);

        $("#grade option[value='0']").attr("selected","selected");//默認(rèn)第一項(xiàng)被選中
    })
    //為Select添加事件,當(dāng)選擇其中一項(xiàng)時(shí)觸發(fā)
    function showIt(){
        var selectText = $("#grade option:selected").text();//獲取Select選擇的Text
        //var selectText = $("#grade").find("option:selected").text();//這種方式也可行
        var selectValue = $("#grade").val();//獲取被選擇的value
        var selectIndex = $("#grade").get(0).selectedIndex//獲取select的索引值
        var text = '選擇:'+selectText+"/n";
        text +='value值:'+selectValue+"/n";
        text +='索引值:'+selectIndex;
        $("#text").text(text);
    }
</script>
</head>
<body>
    <div>
        <select name='grade' id='grade' onchange="showIt()"></select>
        <p><textarea name='text' id='text' row='30' col='100'></textarea></p>
    </div>
</body>
</html>

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江门市| 利津县| 江安县| 宁武县| 加查县| 诏安县| 佛坪县| 富裕县| 西峡县| 丽江市| 商水县| 南充市| 榕江县| 盐源县| 岑巩县| 武义县| 偏关县| 巴东县| 江阴市| 清河县| 剑川县| 鹤山市| 尉氏县| 天水市| 荆门市| 花莲市| 富顺县| 芮城县| 广水市| 汝城县| 南江县| 萍乡市| 高雄县| 彰化市| 布尔津县| 永仁县| 林西县| 武宣县| 阳城县| 花垣县| 南丰县|