3

我的整个 javascript 都在后面的代码上。我想为后面的代码上的 ajax 数据检索 javascript var EmailId 值。但是因为我必须把它写在双引号中,它会导致很多问题。这是我的代码,

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"]) + "'}\"," + ].请任何人帮助我。欢迎提出建议!

4

1 回答 1

0

如果要在字符串中使用双引号,请使用 "" 而不是 \",并在字符串包围值之前使用 @。所以:

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

[编辑]实际上,在我看来,您根本不应该用双引号将其括起来:

data: {'email': $('#txtEmail').val(), 'ItemId':'" + Convert.ToString(Session["ItemID"]) + "'}," +
于 2013-02-27T22:17:40.543 回答