zoukankan      html  css  js  c++  java
  • Jquery获取已经值的方法 checkbox和input输入框

    一、Jquery获取已经选中的checkbox的几种方式

    1.先看看原生js怎么获取的,首先获取name为某一对象的checkbox对象,此时接收到的是一个数组,里面存储的是所有的数组,再对所有数组进行遍历,如果当前为点击的对象,就添加就定义的空数组中。

    var objetct = document.getElementsByName(“su_check”);
    var check_list = [];
    for (k in object) {
    if (object[k].checked)
    check_list.push(object[k].value);
    }

    2. 该方法对ie浏览器的兼容不太友好,建议将for循环语句更换成如下:for (var i = 0; i < object.length; i++) 

    3. jquery获取已经点击的checkbox复选框

     <body>

            
        <input type="checkbox" id="" name="abc" value="123">
        <input type="checkbox" id="" name="abc" value="456">
        <input type="checkbox" id="" name="abc" value="789">
        <button id="btn">点我</button>


    </body>
    </html>
    <script src="jquery.min.js"></script>
    <script>
        $(function(){
           $("#btn").click(function(){
               var arr=[];
                 $("input[name='abc']:checked").each(function(){
                arr.push($(this).val());
                 })
                // console.log(arr); //在控制台输出
                for(i in arr){
                    alert(arr[i]); //弹出复选框中的值
                }
            })
            
        })
     </script>

     二、js获取input输入框中的值

    <body>
        <input type="text" id="text">
        <button id="btn">提交</button>
    </body>
    <script>
     
        document.getElementById("btn").onclick=function(){
            var text = document.getElementById("text").value;
            alert(text);
        }
    </script>    
  • 相关阅读:
    人工智能数学基础笔记(上)
    人工智能简介
    十三,十四 基金收益,税收与基金国际化
    资产配置模型之-BL模型
    十二 基金估值,费用与会计核算
    十一 基金的投资交易与结算
    十 基金业绩评价
    九 投资风险管理
    浙工商oj ___飞龙的飞行方程
    hd1004解题思路
  • 原文地址:https://www.cnblogs.com/starwei/p/12459934.html
Copyright © 2011-2022 走看看