最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2014年01月08日 10:16:29   作者:  
本篇文章主要是對(duì)jquery對(duì)單選框,多選框,文本框等常見操作進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助

一、文本框、單選按鈕、復(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>

相關(guān)文章

最新評(píng)論

绥芬河市| 黄梅县| 泰安市| 临夏市| 开化县| 武冈市| 灵山县| 乌拉特中旗| 东光县| 建湖县| 丁青县| 达日县| 航空| 望都县| 库伦旗| 黄平县| 双牌县| 建始县| 巨野县| 鄂伦春自治旗| 凤翔县| 天峻县| 邓州市| 常宁市| 平顺县| 桂平市| 工布江达县| 商洛市| 襄汾县| 额敏县| 射阳县| 海盐县| 景德镇市| 文山县| 普宁市| 金塔县| 洪湖市| 八宿县| 临清市| 龙州县| 乌兰察布市|