2010-04-15 46 views
0

我使用几个JSON调用来渲染数据等等。为了保持正确的键值,我将它存储在一个标记中。在jQuery(和ASP.NET MVC)中使用隐藏值 - 不工作?

我在我的代码中有几个地方有这个,其中没有一个导致像这样的问题。这里是jQuery的:

的称之为 “套” 的值:

$("a[id^='planSetupAddNewPlan']").live('click', function() { 
     var x = $(this).attr('id'); 
     x = x.substring(19); 

     $("#hidPlanSetupCurrentGroupKey").val(x); 

     $.getJSON("/GroupSetup/PlanSetupAddNewList", { GroupKey: x }, function(data) { 
      $("#planSetupAddNew").html('' + data.TableResult + ''); 


      alert('First Inside 2 ' + x); 

      $.blockUI({ message: $("#planSetupAddNew") }); 
     }); 
    }); 

说, “获得” 价值召唤:

$("#ddlPlanSetupAddNewProduct").live('change', function() { 
      var a = $("#hidPlanSetupCurrentGroupKey").val(); 
      var prod = $(this).val(); 

      alert(a); 

      $.getJSON("/GroupSetup/PlanSetupChangePlanList", { GroupKey: a, Product: prod }, function(data) { 
       if (data.Message == "Success") { 
        $("#planSetupAddNewPlan").html('' + data.TableResult + ''); 
       } else if (data.Message == "Error") { 
        //Do something 
       } 
      }); 
     }); 

这里是有问题的HTML:

<div id="planSetupAddNew" style="display:none; cursor: default;"> 
    <input type="hidden" id="hidPlanSetupCurrentGroupKey" /> 
    <div id="planSetupAddNewData"> 

    </div> 
</div> 

在第一部分中,alert('First Inside 2'+ x)返回我期望的值(其中x =键值),如果我添加一行显示隐藏字段的内容,也适用于:

ie。

var key = $("#hidPlanSetupCurrentGroupKey").val(); 
alert(key); 

在“警报(a)”中,电话,我得到“未定义”。我在同一视图中查看了其他代码,并且它是相同的,并且工作正常。我必须错过某些东西,或者有某种我没有发现的错误类型。

只是控制器事件的​​概述: 第一个调用(/ GroupSetup/PlanSetupAddNewList)将返回一个构建“表单”的html字符串,供用户输入信息。

第二个调用(/ GroupSetup/PlanSetupChangePlanList)仅基于第一个下拉选择(覆盖div中的html)更改第二个下拉列表。

如果您需要更多信息,请告诉我!

任何想法/提示/指针/建议?!?!

感谢您的帮助:)

回答

2

为什么不使用普通的javascript全局变量? 或者使用jQuery的data

var globalGroupKey = ''; 
$("a[id^='planSetupAddNewPlan']").live('click', function() { 
    var x = $(this).attr('id'); 
    globalGroupKey = x.substring(19); 
    $.getJSON("/GroupSetup/PlanSetupAddNewList", { GroupKey: globalGroupKey }, function(data) { 
     $("#planSetupAddNew").html('' + data.TableResult + ''); 


     alert('First Inside 2 ' + x); 

     $.blockUI({ message: $("#planSetupAddNew") }); 
    }); 
}); 

$("#ddlPlanSetupAddNewProduct").live('change', function() { 
     var prod = $(this).val(); 
     alert(globalGroupKey); 
     $.getJSON("/GroupSetup/PlanSetupChangePlanList", { GroupKey: globalGroupKey, Product: prod }, function(data) { 
      if (data.Message == "Success") { 
       $("#planSetupAddNewPlan").html('' + data.TableResult + ''); 
      } else if (data.Message == "Error") { 
       //Do something 
      } 
     }); 
}); 

无论如何,这在我看来就像change事件click之前发射,所以可能没什么的,这将工作。

+0

同意,有点听起来像你在这里重新发明轮子 – 2010-04-15 14:17:51

+0

改变事件将永远不会发生在点击之前。点击绘制一个新的“窗口”(blockui - 模式样式弹出窗口),其中包含change事件的控件。 虽然这工作,感谢全球变量解决方案!这很好! – SlackerCoder 2010-04-15 14:39:52