【问题标题】:unable to access asp.net textbox value in jquery modal dialog无法在 jquery 模式对话框中访问 asp.net 文本框值
【发布时间】:2013-09-12 08:47:10
【问题描述】:

在我的代码中,下拉列表的更改事件会出现模式弹出窗口。弹出窗口包含一个 asp 文本框和按钮。我无法在后面的代码中使用文本框值。我当前的代码为该文本框提供了未定义的值。这是代码快照:

我使用了 avgrund 插件来弹出模式。

<script type="text/javascript">
$(function () {
    $('#Content_ddl_RepCountry').change(function () { // popup called on dropdown change enevt
        var result = $(this).val();

        if (result == "test") {
            $(this).avgrund({ 
                showClose: true,
                showCloseText: 'CLOSE',
                template: $("#modal_div").html(),
                open: function () {
                    var dlg = $('#modal_div').dialog({
                    });
                    dlg.parent().appendTo(jQuery("form:first"));
                }

            });

            }
        else {
            alert('how r');
            this.clearAttributes();            
        }        

    });
});
</script>

调用 ajax 函数将文本框值传递给后面的代码

<script type="text/javascript">
     function new_Fn() {
                $.ajax({
                    type: "POST",
                    url: "Defacement.aspx.cs/childBind",
                    data: {
                        txt1: $("#test_input").val()
                    },
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: alert('successful' + $('#Content_test_input').value)

                });
            }
</script>

在 aspx 页面上包含模态弹出窗口的 Div

<div id="modal_div" style="display: none;"> <%--style="display: none;" --%>
    <table id="tbl_heading" width="100%" height="100%"> 
         <tr>
            <td colspan="2"><span id="heading" class="heading">Add New Country</span></td> 
         </tr> 
         <tr> 
            <td colspan="2"></td> 
         </tr> 
        <tr> 
            <td colspan="2">
                <asp:Label ID="test_label" runat="server" Text="Label"></asp:Label>
            </td> 
         </tr> 
         <tr> 
            <td class="P_td_label"><span id="test_span">Input1</span></td> 
            <td>
                <asp:TextBox ID="test_input" runat="server"></asp:TextBox>
            </td> 
         </tr> 
         <tr> 
            <td colspan="2">

                <asp:Button ID="btn_test" runat="server" Text="Save" CssClass="button" OnClientClick="new_Fn(); return false;" OnClick="btn_test_Click" UseSubmitBehavior="false" />

            </td> 
         </tr> 
     </table>
</div>

在 ajax 函数中调用包含 webmethod 的 C# 代码

[WebMethod]
        public static string childBind(string txt1)
        {
            string res = txt1.ToString();
            return res;
        }

Any help is appreciated.

【问题讨论】:

    标签: c# jquery asp.net


    【解决方案1】:

    弹出框内的文本框是一个服务器端控件。 所以尝试使用:

    $('<%=test_input.ClientID %>').val()
    

    【讨论】:

    • 感谢您的回复。仍然得到“未定义”的文本框值。
    【解决方案2】:

    您需要将值传递给 page.aspx 而不是 .cs 文件,它永远无法访问。

                $.ajax({
                    type: "POST",
                    url: "Defacement.aspx/childBind",
                    data: {
                        txt1: $("#test_input").val()
                    },
    

    【讨论】:

    • 仍然获得“未定义”文本框值。
    【解决方案3】:

    改成这个。

    编辑:注意数据参数中 txt1 周围的引号。

        $.ajax({
                    type: "POST",
                    url: "Defacement.aspx.cs/childBind",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: {"txt1:" + $("#test_input").val()},
    
                    success: function(){alert("successful" + $('#Content_test_input').val()); }
    
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多