![](/img/trans.png)
[英]JQuery / JavaScript - trigger button click from another button click event
[英]How to get values from a button click event to another button click event
我想从div on button click事件中获取值,并在另一个button click事件中获取这些值。但是无法在其他click事件中获取这些值。 这是我的div的代码:
'<div class="dvDynamic_' + pid + '"><span class="count_' + pid + '">' + count + '</span><span id="pname" style = "margin-left:70px;">' + pname + '</span><span id="punitprice" style = "margin-left:150px;">' + uprice + '</span></div>'
首次按钮单击事件的代码:
$(document).ready(function () {
var ids = [];
$("#btnproceed").click(function () {
debugger;
// $("div[class^='apple-']")
$("div[class^='dvDynamic_']").each(function () {
debugger;
var pids = $(this).text();
ids.push(pids);
});
ids = [];
});
第二键代码单击:
$('#btnsubmit').click(function () {
debugger;
var form = [{
"name": "customerinfo",
"value": JSON.stringify($('#customerform'))
}];
var data = JSON.stringify({
'products': ids,
'customerinfo': form
});
$.ajax({
type: "POST",
url: "@Url.Action("
GetIds ", "
Store ")",
data: data,
contentType: "application/json; charset=utf-8",
success: function (r) {
alert(r.d);
}
});
});
现在,您在每次执行$("#btnproceed").click
处理程序时清除数组ids
$("#btnproceed").click
$("#btnproceed").click(function () {
debugger;
// $("div[class^='apple-']")
$("div[class^='dvDynamic_']").each(function () {
debugger;
var pids = $(this).text();
ids.push(pids);
});
ids = []; // here you clear all ids added in previous each loop
});
只需删除此行ids = [];
或将其移至方法开始
$("#btnproceed").click(function () {
ids = []; // here you clear all ids added in previous handler
debugger;
// $("div[class^='apple-']")
$("div[class^='dvDynamic_']").each(function () {
debugger;
var pids = $(this).text();
ids.push(pids);
});
//before method exit - ids array contain data added in previous loop
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.