jquery获取checkbox选中值 这些求和
来源:互联网 发布:安全电脑炒股软件 编辑:程序博客网 时间:2024/05/17 22:00
注意重点:取得的值要转为数字类型(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" onclick="go();" value="提交"/> </div> </body> </html>
阅读全文
0 0
- jquery获取checkbox选中值 这些求和
- JQuery 获取选中checkbox值
- jquery 获取 选中的checkbox的值
- jquery获取checkbox选中的值
- jquery获取checkbox选中的值
- jquery获取checkbox选中的值
- JQuery获取checkbox选中的值
- Jquery获取checkbox选中项的值
- JQuery 怎么获取选中checkbox值
- jQuery获取checkbox选中的值
- jquery获取checkbox选中值为undefined
- Jquery 获取 radio/select/checkbox 选中值
- jQuery获取checkbox选中的值
- jquery获取 checkbox 选中的值
- jQuery获取radio checkbox选中值
- jquery 获取checkbox 选中的值
- jquery如何获取checkbox选中值
- jQuery 获取所有checkbox选中的值 、 全选checkbox等
- ThinkPad黑将S笔记本进bios设置u盘启动教程
- (6) 什么是数据对象--每天五分钟学习数据结构
- 文章标题
- RecyclerView的touch事件
- 统一导航路由方案
- jquery获取checkbox选中值 这些求和
- Android实现沉浸式状态栏之再理解
- float 和int转换是怎么做到的
- 屏幕支持概览
- 快慢指针的应用
- svn配置
- 开发灵活,稳定,可持续 HTML 和 CSS 代码的规范。
- L1-006. 连续因子
- windows server 2008 iis7 asp连接access数据库