天天看點

jquery擷取checkbox選中值 這些求和

注意重點:取得的值要轉為數字類型(Number())

改進展示方式:(傳遞兩個參數)

<!doctype html>  
<html>  
<head>  
<meta charset="utf-8">  
<title>無标題文檔</title>  
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>    
</head>  
  
<body>  

<script>  
$(function(){  
    $("input[name='chbox']").change(function(){  
        var result=0;  
        var pv=0;
        $("input[name='chbox']:checked").each(function(){ 
               //分割取得價格
                st1= $(this).val();
                var strs1= new Array(); //定義一數組   
                strs1=st1.split("|"); //字元分割
                pv=strs1[1];//1|100  1=100
               //把價格相加得出和
               result+=Number(pv);
              // alert($(this).val())

        });  
              //alert(result)
        $("#tinput").val(result);      
    });  
})  
  
</script>  
  
<input type="text" id="tinput"/>  
        <div>  
            <input type="checkbox" name="chbox" value="1|100"/>1  
            <input type="checkbox" name="chbox" value="2|200"/>2  
            <input type="checkbox" name="chbox" value="3|300"/>3  
            <input type="checkbox" name="chbox" value="4|400"/>4  
      
        </div>  
          
    
</body>  
</html>      

展示方式0:改進型

<!doctype html>  
<html>  
<head>  
<meta charset="utf-8">  
<title>無标題文檔</title>  
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>    
</head>  
  
<body>  

<script>  
$(function(){  
    $("input[name='chbox']").change(function(){  
        var result=0;  
        var pv=0;
        $("input[name='chbox']:checked").each(function(){ 

               //把價格相加得出和
               result+=Number($(this).val());
              // alert($(this).val())

        });  
              //alert(result)
        $("#tinput").val(result);      
    });  
})  
  
</script>  
  
<input type="text" id="tinput"/>  
        <div>  
            <input type="checkbox" name="chbox" value="1"/>1  
            <input type="checkbox" name="chbox" value="2"/>2  
            <input type="checkbox" name="chbox" value="3"/>3  
            <input type="checkbox" name="chbox" value="4"/>4  
      
        </div>  
          
    
</body>  
</html>      

展示方式一:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>無标題文檔</title>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>  
</head>

<body>
<script>
$(function(){
    $("input[name='chbox']").change(function(){
        var result="";
        $("input[name='chbox']:checked").each(function(){
               result+=$(this).val()+',';
        });
//        if(result!=""){
//            result=result.substring(0,result.lastIndexOf(',')); 
//        }
    
str=result; //這是一字元串 
var strs= new Array(); //定義一數組 
var sum=0;
strs=str.split(","); //字元分割 
for (i=0;i<strs.length ;i++ ) 
{ 
  sum+=Number(strs[i]);
  // document.write(strs[i]+"<br/>"); //分割後的字元輸出 
} 
    
        $("#tinput").val(sum);    
    });
})



</script>

<input type="text" id="tinput"/>
        <div>
            <input type="checkbox" name="chbox" value="1"/>1
            <input type="checkbox" name="chbox" value="2"/>2
            <input type="checkbox" name="chbox" value="3"/>3
            <input type="checkbox" name="chbox" value="4"/>4
    
        </div>
        
  
</body>
</html>      

展示方式二:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
 <head>  
  <title> New Document </title>  
  <meta name="Generator" content="EditPlus">  
  <meta name="Author" content="">  
  <meta name="Keywords" content="">  
  <meta name="Description" content="">  
 </head>  
 <script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>  
    <script type="text/javascript">  

        function go() {  
            var strs="";  
            $("input[name='checkbox']:checkbox").each(function(){   
                if($(this).attr("checked")){  
                    strs += $(this).val()+","  
                }  
            })  
           // alert(strs);  
            //str.split(",");  
            //alert(str[0]);  
  var sum=0;
strs=strs.split(","); //字元分割 
for (i=0;i<strs.length ;i++ ) 
{ 
  sum= sum+Number(strs[i]);
}
       $("#tinput").val(sum);   
        }  
    </script>  
 <body>  
  <div>  
    <input type="text" id="tinput" value=""/>  
    <input type="checkbox" name="checkbox" value="1"/> 1 
    <input type="checkbox" name="checkbox" value="2"/> 2 
    <input type="checkbox" name="checkbox" value="3"/> 3 
    <input type="checkbox" name="checkbox" value="4"/> 4
    <input type="checkbox" name="checkbox" value="5"/> 5
    <input type="button" id="test" οnclick="go();" value="送出"/>  
  </div>  
 </body>  
</html>      

繼續閱讀