0

我一直在尝试将变量从 JQuery 函数传递到后面的代码,以便我可以通过 SQL 语句将其输入数据库。但是,我似乎从来没有让变量通过。或者如果它正在通过,我似乎无法让它显示以确保它已经通过。谁能看到我的脚本有什么问题?

<script type="text/javascript">
  $(function () {
      var comment = $("#comment"),
      allFields = $([]).add(comment);
      $('#<%= hidden.ClientID %>').val(comment);


      $("#dialog-form").dialog({
          autoOpen: false,
          height: 300,
          width: 350,
          modal: true,
          buttons: {
              "Add Comment": function () {
                  var bValid = true;
                  allFields.removeClass("ui-state-error");
                  if (bValid) {
                      $("#comments tbody").append("<tr>" +
                        "<td>" + comment.val() + "</td>" +
                        "<td>" + "<%=currentUser%>" + "</td>" +
                        "</tr>");

                      $(this).dialog("close");
                      //                          
                  }
              },
              Cancel: function () {
                  $(this).dialog("close");
              }
          },
          close: function () {
              allFields.val("").removeClass("ui-state-error");
          }
      });

      $("#NewComment")
    .button()
    .click(function () {
        $("#dialog-form").dialog("open");

    });
  });  
    </script>  

这是我隐藏的输入字段:

<input id="hidden" type="hidden" runat="server" />
4

2 回答 2

3

我建议使用这样的实际 Asp.NetHiddenField控件:

标记

<div>
    <asp:HiddenField ID="hidden" runat="server" />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>

代码背后

protected void Button1_Click(object sender, EventArgs e)
{
    string hidden = this.hidden.Value;
    Response.Write(hidden);
}

Javascript

<script>
    $(document).ready(function () {
        $("#<%=hidden.ClientID %>").val("Hello, World!");
    });
</script>

输出:你好,世界!

于 2013-01-31T23:24:09.090 回答
0

What are you trying to do with this line: $('#<%= hidden.ClientID %>').val(comment); ? aren't you missing something like comment**.val()**

Still if I were you I would use a simple handler and make a ajax request.

于 2013-01-31T23:08:26.270 回答