0

我有一个 asp.net 按钮:

 <asp:Button ID="MainContent_ibSave" runat="server" Text="Save" />
                </td>

我需要保留它,runat=server因为单击它时我必须处理一些代码。但是在jquery中我有这个:

 $("#MainContent_ibSave").click(function () {
            if ($('#MainContent_txtShipToName').val().length == 0) {
                $('#hErrorsExist').val("1");
                $('#error').show();
            }
            else {
                $('#hErrorsExist').val("0");
                $('#error').hide();
            }

基本上我在这里所做的只是检查是否在#MainContent_txtShipToName 中输入了任何文本。如果没有输入任何内容,我想抛出错误消息。为此,我想我会添加一个隐藏字段:

<input id="hErrorsExist" type="hidden" />

维护页面上是否存在错误的状态。因此,如果表单上存在错误,我可以将值设置为 1,否则我将其设置为 0。

单击此按钮后,它将值设置为 1 显示#error(这只是一个 div),但随后 div 消失了。就好像回发已经重置了 hErrorsExist 的值......

我什至在我的 jquery 中添加了一个检查:

  if ($('#hErrorsExist').val() == "0" || $('#hErrorsExist').val().length == 0) {
            alert("about to hide");
            $('#error').hide();
            alert($('#hErrorsExist').val());
        }
        else {
            $('#error').show();
            alert($('#hErrorsExist').val());
        }

这是我的文档准备功能中的第一件事。我不知道如何处理这个问题,以便即使在按钮回发后 div #error 也会保持不变。如果我确实输入了一个值并且它验证了它应该将 hErrorsExist 设置为 0 并隐藏 div #error 并在回发后保持 div 隐藏。

这是完整的jquery:

 $(document).ready(function () {
        /*hide message container on top*/
        //alert($('#hErrorsExist').val().length);
        if ($('#hErrorsExist').val() == "0" || $('#hErrorsExist').val().length == 0) {
            alert("about to hide");
            $('#error').hide();
            alert($('#hErrorsExist').val());
        }
        else {
            $('#error').show();
            alert($('#hErrorsExist').val());
        }

          $("#MainContent_ibSave").click(function () {
            if ($('#MainContent_txtShipToName').val().length == 0) {
                $('#hErrorsExist').val("1");
                alert("Setting to 1");
                alert($('#hErrorsExist').val());
                $('#error').show();
            }
            else {
                $('#hErrorsExist').val("0");
                alert("Setting to 0");
                alert($('#hErrorsExist').val());
                $('#error').hide();
            }
        });
          }); 
4

1 回答 1

1

你试过这个吗?

$("#<%= MainContent_ibSave.ClientID %>").click(function (arg) {
   arg.preventDefault();
   if ($('#MainContent_txtShipToName').val().length == 0) {
       $('#hErrorsExist').val("1");
       $('#error').show();
   }
   else {
       $('#hErrorsExist').val("0");
       $('#error').hide();
   }
});
于 2012-04-19T21:26:08.307 回答