简体   繁体   English

如何从按钮单击事件获取值到另一个按钮单击事件

[英]How to get values from a button click event to another button click event

I want to get values from a div on button click event and get those values in another button click event.But not able to fetch those values in other click's event. 我想从div on button click事件中获取值,并在另一个button click事件中获取这些值。但是无法在其他click事件中获取这些值。 here is code of my 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>'

Code of First Button click event: 首次按钮单击事件的代码:

$(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 = [];
    });

Code of Second button click : 第二键代码单击:

$('#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);
        }
    });
});

now you clear array ids on each execute $("#btnproceed").click handler 现在,您在每次执行$("#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
});

just remove this line ids = []; 只需删除此行ids = []; or move this to method start 或将其移至方法开始

$("#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM