JQuery對表單元素的基本操作使用總結(jié)

字號:


    這篇文章主要介紹了JQuery對表單元素的基本操作使用總結(jié),需要的朋友可以參考下。
    select下拉列表onChange事件之JQuery實現(xiàn):
    JQuery:
    $(document).ready(function () {
    $("#selectMenu").bind("change", function () {
    if ($(this).val() == "pro1") {
    $("#pro1").slideDown();
    $("#pro2").slideUp();
    }
    else if($(this).val() =="pro2") {
    $("#pro2").slideDown();
    $("#pro1").slideUp();
    }
    });
    });
    HTML:
    <select id="selectMenu">
    <option value="" >Please select product below</option>
    <option value="pro1">Product 1</option>
    <option value="pro2">Product 2</option>
    </select>
    //1.jQuery對select的基本操作
    $("#select_id").change(function(){ //code...}); //為Select添加事件,當(dāng)選擇其中一項時觸發(fā)
    var checkValue=$("#select_id").val(); //獲取Select選擇的Value
    var checkValue = $('.formc select[@name="country"]').val(); //得到下拉菜單name=country的選中項的值
    var checkValue=$("#select_id").val().join(","); //獲取select多選(multiple="true"時候) 的value
    var checkText = $("#select_id").find("option:selected").text(); //獲取Select選擇的Text
    var checkText = $("select[@name=country] option[@selected]").text(); //獲取select被選中項的文本(注意中間有空格)
    var checkText = $("#select_id option:selected").text();
    var cc2 = $('.formc select[@name="country"]').val(); //得到下拉菜單的選中項的值
    var cc3 = $('.formc select[@name="country"]').attr("id"); //得到下拉菜單的選中項的ID屬性值
    var checkIndex=$("#select_id ").get(0).selectedIndex; //獲取Select選擇的索引值
    var maxIndex=$("#select_id option:last").attr("index"); //獲取Select最大的索引值
    $("#select_id ").get(0).selectedIndex = 1; //設(shè)置Select索引值為1(第二項)的項選中
    $('#select_id')[0].selectedIndex = 1; //設(shè)置Select索引值為1(第二項)的項選中
    $("#select_id ").val(4); //設(shè)置Select的Value值為4的項選中
    $("#select_id option[text='jQuery']").attr("selected", true); //設(shè)置Select的Text值為jQuery的項選中
    $("#select_id").attr("value",'-sel3'); //設(shè)置value=-sel3的項目為當(dāng)前選中項
    $("#select_id").empty(); //清空下拉框
    $("#select_id").append("<option value='Value'>Text</option>"); //為Select追加一個Option(下拉項)
    $("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#select_id")//添加下拉框的option
    $("#select_id").prepend("<option value='0'>請選擇</option>"); //為Select插入一個Option(第一個位置)
    $("#select_id option:last").remove(); //刪除Select中索引值最大Option(最后一個)
    $("#select_id option[index='0']").remove(); //刪除Select中索引值為0的Option(第一個)
    $("#select_id option[value='3']").remove(); //刪除Select中Value='3'的Option
    $("#select_id option[text='4']").remove(); //刪除Select中Text='4'的Option
    //2.jquery對radio的基本操作
    var item = $('input[@name=items][@checked]').val(); //獲取一組radio被選中項的值
    var rval = $("input[@type=radio][@checked]").val(); //得到單選框的選中項的值(注意中間沒有空格)
    $('input[@name=items]').get(1).checked = true; //radio單選組的第二個元素為當(dāng)前選中值
    $("input[@type=radio]").attr("checked",'2'); //設(shè)置value=2的項目為當(dāng)前選中項
    $("input[@type=radio][@value=2]").attr("checked",'checked'); //設(shè)置單選框value=2的為選中狀態(tài).(注意中間沒有空格)
    //3.jquery對checkbox的基本操作
    $("#checkbox_id").attr("value"); //多選框checkbox
    $("input[@type=checkbox][@checked]").val(); //得到復(fù)選框的選中的第一項的值
    $("input[@type=checkbox][@checked]").each(function(){ //由于復(fù)選框一般選中的是多個,所以可以循環(huán)輸出
    alert($(this).val());
    });
    $("#chk1").attr("checked",'');//不打勾
    $("#chk2").attr("checked",true);//打勾
    if($("#chk1").attr('checked')==undefined) //判斷是否已經(jīng)打勾
    //4.jquery對text的基本操作
    $("#txt").attr("value"); //文本框,文本區(qū)域:
    $("#txt").attr("value",''); //清空內(nèi)容
    $("#txt").attr("value",'11');//填充內(nèi)容