zoukankan      html  css  js  c++  java
  • jQuery对表单元素的取值和赋值操作代码

    使用常规的思路:$(“#keyword”).value 取值是取不到的,因为此时$(‘#keydord’)已经不是个element,而是个jquery对象,所以应该使用:$(“#keyword”).val()
     
     
    $("#keyword")[0].value = ""; 


    /*获得TEXT.AREATEXT的值*/ 
    var textval = $("#text_id").attr("value"); 
    //或者 
    var textval = $("#text_id").val(); 

    /*获取单选按钮的值*/ 
    var valradio = $("input[type=radio]:checked").val(); 

    /*获取一组名为(items)的radio被选中项的值*/ 
    var item = $('input[name=items]:checked').val(); 

    /*获取复选框的值*/ 
    var checkboxval = $("#checkbox_id").attr("value"); 

    /*获取下拉列表的值*/ 
    var selectval = $('#select_id').val(); 

    /*文本框,文本区域*/ 
    $("#text_id").attr("value",");//清空内容 
    $("#text_id").attr("value",'test');//填充内容 

    /*多选框checkbox*/ 
    $("#chk_id").attr("checked",");//使其未勾选 
    $("#chk_id").attr("checked",true);//勾选 
    if($("#chk_id").attr('checked')==true) //判断是否已经选中 

    /*单选组radio*/ 
    $("input[type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项 

    /*下拉框select*/ 
    $("#select_id").attr("value",'test');//设置value=test的项目为当前选中项 
    $("testtest2").appendTo("#select_id")//添加下拉框的option 
    $("#select_id").empty();//清空下拉框 

    /*获取一组名为(items)的radio被选中项的值*/ 
    var item = $('input[name=items]:checked').val(); //若未被选中 则val() = undefined 

    /*获取select被选中项的文本*/ 
    var item = $("select[name=items] option:selected").text(); 

    /*select下拉框的第二个元素为当前选中值*/ 
    $('#select_id')[0].selectedIndex = 1; 

    /*radio单选组的第二个元素为当前选中值*/ 
    $('input[name=items]').get(1).checked = true; 

    /*重置表单*/ 
    $("form").each(function(){ 
    .reset(); 
    }); 
    jQuery对html元素取值与赋值 

    Jquery给基本控件的取值、赋值 TEXTBOX: var str = $('#txt').val(); $('#txt').val("Set Lbl Value"); //文本框,文本区域: $("#text_id").attr("value",'');//清空内容 $("#text_id").attr("value",'test');// 填充内容 
    Jquery给基本控件的取值、赋值 

    TEXTBOX: 
    var str = $('#txt').val(); 

    $('#txt').val("Set Lbl Value"); 

    //文本框,文本区域: 
    $("#text_id").attr("value",'');//清空内容 
    $("#text_id").attr("value",'test');// 填充内容 
    LABLE: 

    var str = $('#lbl').text(); 

    $('#lbl').text("Set Lbl Value"); 

    var valradio = $("input[@type=radio][@checked]").val(); 
    var item = $('input[@name=items][@checked]').val(); 
    var checkboxval = $("#checkbox_id").attr("value"); 
    var selectval = $('#select_id').val(); 
    //多选框checkbox: 
    $("#chk_id").attr("checked",'');//使其未勾选 
    $("#chk_id").attr("checked",true);// 勾选 
    if($("#chk_id").attr('checked')==true) //判断是否已经选中 
    单选组radio: 

    $("input[@type=radio]").attr("checked",'2'); //设置value=2的项目为当前选中项 

    //下拉框select: 
    $("#select_id").attr("value",'test');// 设置value=test的项目为当前选中项 
    $("<option value='test'>test</option><option value='test2'>test2</option>").appendTo("#select_id")//添加下拉框的 option 
    $("#select_id").empty();//清空下拉框 

    获取一组名为 (items)的radio被选中项的值 
    var item = $('input[@name=items][@checked]').val();//若未被选中 则val() = undefined 
    获 取select被选中项的文本 
    var item = $("select[@name=items] option[@selected]").text(); 
    select下拉框的第二个元素为当前选中值 
    $('#select_id')[0].selectedIndex = 1; 
    radio单选组的第二个元素为当前选中值 
    $('input[@name=items]').get(1).checked = true; 

    //重置表单 
    $("form").each(function(){ 
    .reset(); 
    }); 
    摘要: Jquery给基本控件的取值、赋值 TEXTBOX: var str = $('#txt').val(); $('#txt').val("Set Lbl Value"); //文本框,文本区域: $("#text_id").attr("value",'');//清空内容 $("#text_id").attr("value",'test');// 填充内容 
    Jquery给基本控件的取值、赋值 

    TEXTBOX: 
    var str = $('#txt').val(); 

    $('#txt').val("Set Lbl Value"); 

    //文本框,文本区域: 
    $("#text_id").attr("value",'');//清空内容 
    $("#text_id").attr("value",'test');// 填充内容 
    LABLE: 

    var str = $('#lbl').text(); 

    $('#lbl').text("Set Lbl Value"); 

    var valradio = $("input[@type=radio][@checked]").val(); 
    var item = $('input[@name=items][@checked]').val(); 
    var checkboxval = $("#checkbox_id").attr("value"); 
    var selectval = $('#select_id').val(); 
    //多选框checkbox: 
    $("#chk_id").attr("checked",'');//使其未勾选 
    $("#chk_id").attr("checked",true);// 勾选 
    if($("#chk_id").attr('checked')==true) //判断是否已经选中 
    单选组radio: 

    $("input[@type=radio]").attr("checked",'2'); //设置value=2的项目为当前选中项 

    //下拉框select: 
    $("#select_id").attr("value",'test');// 设置value=test的项目为当前选中项 
    $("<option value='test'>test</option><option value='test2'>test2</option>").appendTo("#select_id")//添加下拉框的 option 
    $("#select_id").empty();//清空下拉框 

    获取一组名为 (items)的radio被选中项的值 
    var item = $('input[@name=items][@checked]').val();//若未被选中 则val() = undefined 
    获 取select被选中项的文本 
    var item = $("select[@name=items] option[@selected]").text(); 
    select下拉框的第二个元素为当前选中值 
    $('#select_id')[0].selectedIndex = 1; 
    radio单选组的第二个元素为当前选中值 
    $('input[@name=items]').get(1).checked = true; 

    //重置表单 
    $("form").each(function(){ 
    .reset(); 
    });
  • 相关阅读:
    LeetCode 769. Max Chunks To Make Sorted
    LeetCode 845. Longest Mountain in Array
    LeetCode 1059. All Paths from Source Lead to Destination
    1129. Shortest Path with Alternating Colors
    LeetCode 785. Is Graph Bipartite?
    LeetCode 802. Find Eventual Safe States
    LeetCode 1043. Partition Array for Maximum Sum
    LeetCode 841. Keys and Rooms
    LeetCode 1061. Lexicographically Smallest Equivalent String
    LeetCode 1102. Path With Maximum Minimum Value
  • 原文地址:https://www.cnblogs.com/zhangdashao/p/4709761.html
Copyright © 2011-2022 走看看