js中给变量赋值后怎么把变量的值先留着再一块传给后台

qq_39687238 2017-08-11 07:39:42
如题,我设置了几个button按钮,点击一个给一个变量赋值, 但是我有好几个变量,想等着按钮点击完了一块传给后台,我应该怎么处理。
...全文
779 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq503959193 2017-08-14
  • 打赏
  • 举报
回复
学习学习学习学习
????22? 2017-08-12
  • 打赏
  • 举报
回复
写个表单不就好了,js控制提交
qq_39687238 2017-08-11
  • 打赏
  • 举报
回复
明白了各位,我引入的的一个js文件的事。。。。。。。 <script type="text/javascript" src="js/shanxuan.js"></script> shanxuan.js: $.fn.sx = function(can) { can = $.extend({ nuv: ".zj",//筛选结果 zi: "sx_child",//所有筛选范围内的子类 qingchu:'.qcqb',//清除全部 over:'on'//选中状态样式名称 }, can || {}); /*删选过程*/ $(this).find('a').click(function() { var url = window.location.href; var id = $(this).attr('rel'); var id2 = $(this).attr('name'); var index = $(this).index(); var num = $(this).parent().find('a').length; var num2 = $(this).parents(can).length; if($(this).attr('class').replace(can.zi,'').replace(/ /g,'')=="on"){ var url = window.location.href; var pddq = $(this).attr('name') + "=" + $(this).attr('rel'); if (url.split('&').length < 2) { url = url.replace('?' + pddq, '') } else { if (url.split(pddq)[0].split('&').length < 2) { url = url.replace(pddq + '&', '') } else { url = url.replace('&' + pddq, '') } } }else{ if (url.split('?').length < 2) { url = url + "?" + id2 + "=" + id } else { if (url.split('&').length < 2) { if (url.split('?')[1].split('=')[0] == id2) { url = url.replace(url.split('?')[1], id2 + "=" + id) } else { url = url + "&" + id2 + "=" + id } } else { if (url.split(id2).length > 1) { if (url.split(id2)[0].split('&').length < 2) { url = url.replace(url.split(id2)[1].split('&')[0], "=" + id) } else { if (url.split(id2)[1].split('&').length < 2) { url = url.replace(id2 + url.split(id2)[1], id2 + "=" + id) } else { url = url.replace(id2 + url.split(id2)[1].split('&')[0], id2 + "=" + id) } } } else { url = url + "&" + id2 + "=" + id } } } } window.location.href = url }) window.onload = function() { /*选中*/ $("."+can.zi).each(function() { var url = window.location.href; var pddq = $(this).attr('name') + "=" + $(this).attr('rel'); if (url.split(pddq).length > 1) { $(this).addClass('on'); $(can.nuv).find(can.qingchu).before("<a rel=" + $(this).attr('rel') + " name=" + $(this).attr('name') + " href='javascript:;'>" + $(this).siblings('span').text() + $(this).text() + "</a> ") } else { $(this).removeClass('on') } }) /*清除全部按钮是否显示*/ var url = window.location.href; if(url.split('?').length>1){ $(can.qingchu).show(); }else{ $(can.qingchu).hide(); } /*点击清除*/ $(can.nuv).find('a').click(function() { var url = window.location.href; var pddq = $(this).attr('name') + "=" + $(this).attr('rel'); if (url.split('&').length < 2) { url = url.replace('?' + pddq, '') } else { if (url.split(pddq)[0].split('&').length < 2) { url = url.replace(pddq + '&', '') } else { url = url.replace('&' + pddq, '') } } window.location.href = url; }) /*清除全部*/ $(can.qingchu).click(function(){ var url = window.location.href; url = url.split('?')[0]; window.location.href = url; }) } }
qq_39687238 2017-08-11
  • 打赏
  • 举报
回复
这是为啥???而且去掉<a> 换了个 <button onclick="testget('111')">确定</button> 在 jsp里也是对的
qq_39687238 2017-08-11
  • 打赏
  • 举报
回复
找到原因了!。。。保存为html文件就对 保存为jsp在apache-tomcat上运行就不对。。。。。。。。。。。。。
引用 13 楼 showbo 的回复:
引用 12 楼 qq_39687238 的回复:
[quote=引用 7 楼 qq_39687238 的回复:] 有点长哦。。。。。。。。。
后面有一部分是没用的代码
你这个没问题,只要你先点击分类,在点击确认,反过来会undefined[/quote]找到原因了!。。。保存为html文件就对 保存为jsp在apache-tomcat上运行就不对。。。。。。。。。。。。。
引用 14 楼 jslang 的回复:
你代码我测试没问题
找到原因了!。。。保存为html文件就对 保存为jsp在apache-tomcat上运行就不对。。。。。。。。。。。。。
weixin_39818549 2017-08-11
  • 打赏
  • 举报
回复
引用 13 楼 showbo 的回复:
引用 12 楼 qq_39687238 的回复:
[quote=引用 7 楼 qq_39687238 的回复:] 有点长哦。。。。。。。。。
后面有一部分是没用的代码
你这个没问题,只要你先点击分类,在点击确认,反过来会undefined[/quote]找到原因了!。。。保存为html文件就对 保存为jsp在apache-tomcat上运行就不对。。。。。。。。。。。。。
qq_39687238 2017-08-11
  • 打赏
  • 举报
回复
引用 14 楼 jslang 的回复:
你代码我测试没问题
奇了怪了啊。。。。
qq_39687238 2017-08-11
  • 打赏
  • 举报
回复
引用 13 楼 showbo 的回复:
引用 12 楼 qq_39687238 的回复:
[quote=引用 7 楼 qq_39687238 的回复:] 有点长哦。。。。。。。。。
后面有一部分是没用的代码
你这个没问题,只要你先点击分类,在点击确认,反过来会undefined[/quote]。。奇了怪了
qq_39687238 2017-08-11
  • 打赏
  • 举报
回复
卧槽 你们都没问题啊 为啥 我第一个第二个弹出来是数字,第三个再点确定就是undefined啊
天际的海浪 2017-08-11
  • 打赏
  • 举报
回复
你代码我测试没问题
Go 旅城通票 2017-08-11
  • 打赏
  • 举报
回复
引用 12 楼 qq_39687238 的回复:
引用 7 楼 qq_39687238 的回复:
有点长哦。。。。。。。。。
后面有一部分是没用的代码

你这个没问题,只要你先点击分类,在点击确认,反过来会undefined
qq_39687238 2017-08-11
  • 打赏
  • 举报
回复
引用 7 楼 qq_39687238 的回复:
有点长哦。。。。。。。。。 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript" src="js/jquery-1.7.2.min.js" ></script> </head> <style type="text/css"> *{margin:0;padding:0;list-style-type:none;} a,img{border:0;text-decoration:none;} body{font:12px/180% Arial, Helvetica, sans-serif, "新宋体";} .demo{width:620px;margin:40px auto 0 auto;} .sx,.zj{padding-bottom:10px;} .sx a{color:#000;line-height:22px;display:inline-block;width:50px;height:22px;margin-right:10px;border:1px solid #fff;text-align:center;padding-right:6px;} a.on{border:1px solid red;background:url(images/xx.jpg) no-repeat right top;} .sx span,.zj span{display:inline-block;line-height:22px;height:22px;color:#00F;font-size:14px;margin-right:10px;text-align:right;width:70px;} .zj a{color:#fff;line-height:22px;display:inline-block;width:100px;height:22px;margin-right:10px;border:1px solid #C00;text-align:center;background:url(images/xx2.jpg) no-repeat right top #C00;padding-right:10px;} .qcqb{width:70px;height:22px;line-height:22px;background:#FF0;color:#690;display:none;display:inline-block;text-align:center;border:1px solid #ccc;cursor:pointer;} </style> <body> <div class="demo"> <div class="sx"> <span>分类1:</span> <a onclick="testget(this.rel)" rel="1" class="sx_child" name="aaa" href="javascript:;">1111</a> <a onclick="testget(this.rel)" rel="2" class="sx_child" name="aaa" href="javascript:;">2222</a> <a onclick="testget(this.rel)" rel="3" class="sx_child" name="aaa" href="javascript:;">3333</a> <a onclick="testget(this.rel)" rel="4" class="sx_child" name="aaa" href="javascript:;">4444</a> </div> <div class="zj"> <span>删选结果:</span> <p class="qcqb">清除全部</p> </div> </div> <button onclick="test()">确定</button> <script type="text/javascript"> var str1; function testget (str) { alert(str); str1=str; alert(str1); } function test() { alert(str1); } </script> <script type="text/javascript"> </script> <script type="text/javascript" src="js/shanxuan.js"></script> <script type="text/javascript"> $('.sx').sx( { nuv:".zj",//筛选结果 zi:"sx_child",//所有筛选范围内的子类 qingchu:'.qcqb',//清除全部 over:'on'//选中状态样式名称 // var test = "....";   // 需要提交的变量 // document.getElementById("").value = test } ); </script> <script type="text/javascript"> var data=[ { "id": 1, "text": 1 }, { "id": 2, "text": 2 }, { "id": 3, "text": 3 }, { "id": 4, "text": 4 }] //例子1 //单选 $('#demo1').comboboxfilter({ url: '', scope: 'FilterQuery1', data:data, onChange:function(newValue){ $('#demo_value').val(newValue); } }); $('#demo2').comboboxfilter({ url: '', scope: 'FilterQuery2', multiple: true, data:data, onChange:function(newValue){ $('#demo_value').val(newValue); } }); $('#demo3').comboboxfilter({ url: '', : 'FilterQuery3', data:data, onChange:function(newValue){ $('#demo_value').val(newValue); } }); $('#demo4').comboboxfilter({ url: '', scope: 'FilterQuery3', multiple: true, data:data, onChange:function(newValue){ $('#demo_value').val(newValue); } }); $('#demo5').comboboxfilter({ url: '', scope: '', unlimitText:'全部', data:data, onChange:function(newValue){ $('#demo_value').val(newValue); } }); $('#demo6').comboboxfilter({ url: '', scope: '', unlimitText:'全部', multiple: true, data:data, onChange:function(newValue){ $('#demo_value').val(newValue); } }); </script> </body> </html>
后面有一部分是没用的代码
二月十六 2017-08-11
  • 打赏
  • 举报
回复
var str1= [];
function testget (str)  
{      
        alert(str);   
        str1.psuh(STR);
        alert(str1);
}
qq_39687238 2017-08-11
  • 打赏
  • 举报
回复
引用 6 楼 jslang 的回复:
可能是你哪里弄错了,发下完整代码,或者试试我这个

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
	<title> 页面名称 </title>
</head>
<body>
<input type="button" value="保存" onclick="saveStr('abc');" />
<input type="button" value="读取" onclick="loadStr();" />
<script type="text/javascript">
var str1;
function saveStr(str) {
	str1 = str;
}
function loadStr() {
	alert(str1);
}
</script>
</body>
</html>
嗯嗯 你的可以的 我估计我用的a标签不对。。。
qq_39687238 2017-08-11
  • 打赏
  • 举报
回复
引用 8 楼 sinat_28984567 的回复:
定义一个全局的数据,每次都存入值,不要定义局部的数组
引用 5 楼 showbo 的回复:
- - [quote=引用 4 楼 qq_39687238 的回复:] [quote=引用 3 楼 showbo 的回复:] 感觉你函数调用顺序有问题,要是调用了testget先,test肯定可以获取到str的值,而不是undefined,最好发你代码全来,不要发什么截图,特别讨厌 是不是调用testget的控件是放在表单中的提交按钮,导致表单提交了刷新一次所以str又是undefined了 推荐学习资料 jqGrid一些疑难问题和解决方案 jqGrid中文API
我没有表单,除了截图代码里就是两个按钮,onclick=分别调用了testget和test函数 但是调用test的时候就没值了,也就是说第一个函数调用完str1的值就失去了,我看到有人说每调用一个js函数<script></script>之间就又要走一次,大概就是这个原因导致str1又重新定义了,楼上说用数组可以,我还没试[/quote] 谁说重走一次。。解析完毕后直接进内存里面了,你js删除那个script标签照样没问题,你肯定是哪里代码重新赋值过了 javascript运行机制浅析[/quote] 看下是不是我的a标签有问题。。。
二月十六 2017-08-11
  • 打赏
  • 举报
回复
定义一个全局的数据,每次都存入值,不要定义局部的数组
qq_39687238 2017-08-11
  • 打赏
  • 举报
回复
有点长哦。。。。。。。。。 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript" src="js/jquery-1.7.2.min.js" ></script> </head> <style type="text/css"> *{margin:0;padding:0;list-style-type:none;} a,img{border:0;text-decoration:none;} body{font:12px/180% Arial, Helvetica, sans-serif, "新宋体";} .demo{width:620px;margin:40px auto 0 auto;} .sx,.zj{padding-bottom:10px;} .sx a{color:#000;line-height:22px;display:inline-block;width:50px;height:22px;margin-right:10px;border:1px solid #fff;text-align:center;padding-right:6px;} a.on{border:1px solid red;background:url(images/xx.jpg) no-repeat right top;} .sx span,.zj span{display:inline-block;line-height:22px;height:22px;color:#00F;font-size:14px;margin-right:10px;text-align:right;width:70px;} .zj a{color:#fff;line-height:22px;display:inline-block;width:100px;height:22px;margin-right:10px;border:1px solid #C00;text-align:center;background:url(images/xx2.jpg) no-repeat right top #C00;padding-right:10px;} .qcqb{width:70px;height:22px;line-height:22px;background:#FF0;color:#690;display:none;display:inline-block;text-align:center;border:1px solid #ccc;cursor:pointer;} </style> <body> <div class="demo"> <div class="sx"> <span>分类1:</span> <a onclick="testget(this.rel)" rel="1" class="sx_child" name="aaa" href="javascript:;">1111</a> <a onclick="testget(this.rel)" rel="2" class="sx_child" name="aaa" href="javascript:;">2222</a> <a onclick="testget(this.rel)" rel="3" class="sx_child" name="aaa" href="javascript:;">3333</a> <a onclick="testget(this.rel)" rel="4" class="sx_child" name="aaa" href="javascript:;">4444</a> </div> <div class="zj"> <span>删选结果:</span> <p class="qcqb">清除全部</p> </div> </div> <button onclick="test()">确定</button> <script type="text/javascript"> var str1; function testget (str) { alert(str); str1=str; alert(str1); } function test() { alert(str1); } </script> <script type="text/javascript"> </script> <script type="text/javascript" src="js/shanxuan.js"></script> <script type="text/javascript"> $('.sx').sx( { nuv:".zj",//筛选结果 zi:"sx_child",//所有筛选范围内的子类 qingchu:'.qcqb',//清除全部 over:'on'//选中状态样式名称 // var test = "....";   // 需要提交的变量 // document.getElementById("").value = test } ); </script> <script type="text/javascript"> var data=[ { "id": 1, "text": 1 }, { "id": 2, "text": 2 }, { "id": 3, "text": 3 }, { "id": 4, "text": 4 }] //例子1 //单选 $('#demo1').comboboxfilter({ url: '', scope: 'FilterQuery1', data:data, onChange:function(newValue){ $('#demo_value').val(newValue); } }); $('#demo2').comboboxfilter({ url: '', scope: 'FilterQuery2', multiple: true, data:data, onChange:function(newValue){ $('#demo_value').val(newValue); } }); $('#demo3').comboboxfilter({ url: '', : 'FilterQuery3', data:data, onChange:function(newValue){ $('#demo_value').val(newValue); } }); $('#demo4').comboboxfilter({ url: '', scope: 'FilterQuery3', multiple: true, data:data, onChange:function(newValue){ $('#demo_value').val(newValue); } }); $('#demo5').comboboxfilter({ url: '', scope: '', unlimitText:'全部', data:data, onChange:function(newValue){ $('#demo_value').val(newValue); } }); $('#demo6').comboboxfilter({ url: '', scope: '', unlimitText:'全部', multiple: true, data:data, onChange:function(newValue){ $('#demo_value').val(newValue); } }); </script> </body> </html>
天际的海浪 2017-08-11
  • 打赏
  • 举报
回复
可能是你哪里弄错了,发下完整代码,或者试试我这个

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
	<title> 页面名称 </title>
</head>
<body>
<input type="button" value="保存" onclick="saveStr('abc');" />
<input type="button" value="读取" onclick="loadStr();" />
<script type="text/javascript">
var str1;
function saveStr(str) {
	str1 = str;
}
function loadStr() {
	alert(str1);
}
</script>
</body>
</html>
Go 旅城通票 2017-08-11
  • 打赏
  • 举报
回复
- -
引用 4 楼 qq_39687238 的回复:
[quote=引用 3 楼 showbo 的回复:] 感觉你函数调用顺序有问题,要是调用了testget先,test肯定可以获取到str的值,而不是undefined,最好发你代码全来,不要发什么截图,特别讨厌 是不是调用testget的控件是放在表单中的提交按钮,导致表单提交了刷新一次所以str又是undefined了 推荐学习资料 jqGrid一些疑难问题和解决方案 jqGrid中文API
我没有表单,除了截图代码里就是两个按钮,onclick=分别调用了testget和test函数 但是调用test的时候就没值了,也就是说第一个函数调用完str1的值就失去了,我看到有人说每调用一个js函数<script></script>之间就又要走一次,大概就是这个原因导致str1又重新定义了,楼上说用数组可以,我还没试[/quote] 谁说重走一次。。解析完毕后直接进内存里面了,你js删除那个script标签照样没问题,你肯定是哪里代码重新赋值过了 javascript运行机制浅析
qq_39687238 2017-08-11
  • 打赏
  • 举报
回复
引用 3 楼 showbo 的回复:
感觉你函数调用顺序有问题,要是调用了testget先,test肯定可以获取到str的值,而不是undefined,最好发你代码全来,不要发什么截图,特别讨厌 是不是调用testget的控件是放在表单中的提交按钮,导致表单提交了刷新一次所以str又是undefined了 推荐学习资料 jqGrid一些疑难问题和解决方案 jqGrid中文API
我没有表单,除了截图代码里就是两个按钮,onclick=分别调用了testget和test函数 但是调用test的时候就没值了,也就是说第一个函数调用完str1的值就失去了,我看到有人说每调用一个js函数<script></script>之间就又要走一次,大概就是这个原因导致str1又重新定义了,楼上说用数组可以,我还没试
加载更多回复(3)

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧