2013-02-19 50 views
3

我的整个JavaScript代码在后面。我想在后面的代码中为我的ajax数据检索javascript var EmailId值。但是因为我必须用双引号将它写出来,所以会导致很多问题。这里是我的代码,从代码后面检索javascript var值

protected void Page_Load(object sender, EventArgs e) 
{ 
    //HttpResponse response = HttpContext.Current.Response; 
    //response.AddHeader("X-Frame-Options", "GOFORIT"); 

    //Response.AddHeader("X-Frame-Options", "GOFORIT"); 
    Session["txtEmail"] = txtEmail; 

    if (!IsPostBack) 
    { 
     GetItemsList(); 
     Session["ProfileImage"] = imgProfilePicture; 

     if (Convert.ToBoolean(Session["GotToken"]) == true) 
     { 
      GetProfilePic(); 
     } 

     if (HttpContext.Current.Request.QueryString["code"] != null) 
     { 
      Session["code"] = HttpContext.Current.Request.QueryString["code"]; 
      string success = TestFB.AllowAppAuthentication(Convert.ToString(Session["ItemID"])); 
      StringBuilder sb = new StringBuilder(); 
      var javacriptVariable = "<%= $('#txtEmail').val() %>"; 
      sb.Append("jQuery(function() { "); 
      sb.Append(@"jQuery('#dialog-form').dialog({ 
           autoOpen: true, 
           height: 500, 
           width: 500, 
           modal: true, 
           buttons: { 
            'Participate': function() { 
             var bValid = true;       
             var EmailId = $('#txtEmail').val(); 

             var chkTermsAndConditions = document.getElementById('chkTermsAndConditions').checked; 
             if (bValid && chkTermsAndConditions == true) { 
              console.log(EmailId); 
              $('p.validateTips').text(''); 
              $.ajax({ 
               type: 'POST', 
               url: 'FitbookContest.aspx/InsertUsersItem', 
               contentType: 'application/json; charset=utf-8', 
               dataType: 'json', 
               data: " + "\"{'email':'" + javacriptVariable + "', 'ItemId':'" + Convert.ToString(Session["ItemID"]) + "'}\"," + 
      " success: function (data) { " + 
      "console.log(EmailId); " + 
      "$('#lblErrorMessage').text(''); " + 
      "document.getElementById('chkTermsAndConditions').checked = false; " + 
      "if (data.d == false) { " + 
      "alert('Sorry!You can participate only once!'); " + 
      "$(this).dialog('close'); " + 
      "} else { " + 
      "alert('Email will be sent to your Account soon!'); " + 
      "$(this).dialog('close'); " + 
      "} " + 
      "}, " + 
      "context: $(this), " + 
      "}); " + 
      "} " + 
      "}, " + 
      "Cancel: function() { " + 
      "document.getElementById('chkTermsAndConditions').checked = false; " + 
      "$(this).dialog('close'); " + 
      "} " + 
      "}, " + 
      "close: function() { " + 
      "$(this).dialog('close'); " + 
      "} " + 
      "});"); 

      sb.Append(" }); "); 
      //ClientScript.RegisterStartupScript(this.GetType(), "popup", sb.ToString(), true); 
      ScriptManager.RegisterStartupScript(this, GetType(), "modalscript", sb.ToString(), true); 
     } 
    } 
} 

我想打开页面加载弹出!并且还想要检索将在弹出窗口的文本框中写入的电子邮件。我只能使用JavaScript检索它。但由于代码后面的双引号无法在“数据”上获得[ data: " + "\"{'email':'" + javacriptVariable + "', 'ItemId':'" + Convert.ToString(Session["ItemID"]) + "'}\"," + ].请任何人帮助我。建议欢迎!

回答

0

如果你想在字符串使用“”,而不是\”双引号,并使用@字符串之前封闭的值,所以:

data: " + @"""{'email':'" + javacriptVariable + "', 'ItemId':'" + Convert.ToString(Session["ItemID"]) + "'}""," + 

[编辑]其实,它看起来像你对我不该不要用双引号括起来:

data: {'email': $('#txtEmail').val(), 'ItemId':'" + Convert.ToString(Session["ItemID"]) + "'}," +