我想从按钮点击事件中的div获取值,并在另一个按钮点击事件中获取这些值。但是无法在其他点击事件中获取这些值。 这是我的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);
}
});
});
答案 0 :(得分:1)
现在您在每个执行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
});