我正在开发的网络应用程序中有以下代码:
var bgcolor = '';
var row = 0;
for(var i = 0; i < result.data.length; i++)
{
// Set alternating background colors
bgcolor = ( i%2 == 0) ? ' style=\'background-color: white;\'': ' style=\'background-color: whitesmoke;\'';
$( "div.row" ).append("\
<div class='search_container'"+bgcolor+">\
<div class='search_result_client_heading'>"+result.data[i]['client_name']+"</div>\
<div class='search_result_industry_heading'>"+result.data[i]['industry_name']+"</div>\
<div class='search_result_yield_heading'>Saved: "+result.data[i]['actual_impact']+" "+result.data[i]['impact_type_name']+" / "+formatCurrency(String(result.data[i]['actual_savings']))+"</div>\
<div class='search_result_problem_heading'>"+nl2br( result.data[i]['problem'] )+"</div>\
<div class='detail_button_holder'>\
<a class='btn search_result_button' id='view_results_"+result.data[i]['record_id']+"' href='#'>View details »</a>\
</div>\
</div>");
$( "#view_results_"+result.data[i]['record_id'] ).click( show_result_dialog(i, result) );
}
然后是一个单独的命名函数,它只是用不同的值填充创建的 div,具体取决于单击的按钮:
function show_result_dialog(row, result)
{
$( "#search-result-dialog-client" ).empty().append("<div class='search_result_label'>Client</div><div class='search_result_value'>"+result.data[row]['client_name']+"</div>");
$( "#search-result-dialog-industry" ).empty().append("<div class='search_result_label'>Industry</div><div class='search_result_value'>"+result.data[row]['industry_name']+"</div>");
$( "#search-result-dialog-contact" ).empty().append("<div class='search_result_label'>Contact</div><div class='search_result_value'>"+result.data[row]['contact_name']+"</div>");
$( "#search-result-dialog-journey" ).empty().append("<div class='search_result_label'>Journey</div><div class='search_result_value'>"+result.data[row]['journey_name']+"</div>");
$( "#search-result-dialog-focus_area" ).empty().append("<div class='search_result_label'>Focus Area</div><div class='search_result_value'>"+result.data[row]['focus_area']+"</div>");
$( "#search-result-dialog-problem" ).empty().append("<div class='search_result_label'>Problem</div><div class='search_result_value'>"+result.data[row]['problem']+"</div>");
$( "#search-result-dialog-approach" ).empty().append("<div class='search_result_label'>Approach</div><div class='search_result_value'>"+result.data[row]['approach']+"</div>");
$( "#search-result-dialog-tactics" ).empty().append("<div class='search_result_label'>Tactics</div><div class='search_result_value'>"+result.data[row]['tactics']+"</div>");
$( "#search-result-dialog-delivery_date" ).empty().append("<div class='search_result_label'>Delivery Date</div><div class='search_result_value'>"+result.data[row]['delivery_date']+"</div>");
$( "#search-result-dialog-impact_type" ).empty().append("<div class='search_result_label'>Impact Type</div><div class='search_result_value'>"+result.data[row]['impact_type_name']+"</div>");
$( "#search-result-dialog-estimated_impact" ).empty().append("<div class='search_result_label'>Estimated Impact</div><div class='search_result_value'>"+result.data[row]['estimated_impact']+" "+result.data[row]['impact_type_name']+"</div>");
$( "#search-result-dialog-actual_impact" ).empty().append("<div class='search_result_label'>Actual Impact</div><div class='search_result_value'>"+result.data[row]['actual_impact']+" "+result.data[row]['impact_type_name']+"</div>");
$( "#search-result-dialog-estimated_savings" ).empty().append("<div class='search_result_label'>Estimated Savings</div><div class='search_result_value'>"+formatCurrency(String(result.data[row]['estimated_savings']))+"</div>");
$( "#search-result-dialog-actual_savings" ).empty().append("<div class='search_result_label'>Actual Savings</div><div class='search_result_value'>"+formatCurrency(String(result.data[row]['actual_savings']))+"</div>");
$( "#search-result-dialog-nps" ).empty().append("<div class='search_result_label'>NPS</div><div class='search_result_value'>"+result.data[row]['nps']+"</div>");
$( "#search-result-dialog-keywords" ).empty().append("<div class='search_result_label'>Keywords</div><div class='search_result_value'>"+result.data[row]['keywords']+"</div>");
$( "#search-result-dialog" ).dialog( "open" );
return false;
}
代码result
中是AJAX调用返回的数据数组(这段代码都在success函数中)
我的问题是点击处理程序在页面加载后立即触发。我希望有人能向我解释为什么会这样,如果有办法解决这个问题,因为我需要i
从循环中传递该函数的值,所以如果我在点击事件中包含一个匿名函数,那么我就有问题关闭,并且总是以最后一个 i 值结束,因此每个按钮都会打开相同的数据。我已经尝试了各种技术但没有成功,包括在主匿名函数中返回一个匿名函数,反之亦然。我在谷歌上花了两天时间试图找到解决方案,但到目前为止没有任何效果。