如何检查文本字段为空,然后调用javascript


How to check the textfield is empty and then call javascript?

当相关文本框为空时,我想查看jquery/javascript对话框。。我尝试了以下代码,但不起作用。。如何在下面的代码中添加if()函数?

      <script>
    $(document).ready(function () {
    $('#search_btn').click(function () {
        if ($('#form1.legacy_code_text.value') == "") { /// THIS DOESNT WORK I WANT THE CORRECT WAY FOR THIS LINE
            $("#dialog:ui-dialog").dialog("destroy");
            $("#dialog-confirm").dialog({
                resizable: false,
                height: 140,
                modal: true,
                buttons: {
                    "Ok": function () {
                        $('#form1').submit();

                        //*****************************************************************
                    },
                    Cancel: function () {
                        $(this).dialog("close");
                    }
                }

            });
        }
    });
});
        </script>
if( $('#legacy_code_text').val() == "" )
if ( empty(#form1.legacy_code_text.value') ) 

这里我已经为上面的问题做了bin。所以,试试看http://codebins.com/bin/4ldqpat

要运行以下脚本,请执行以下步骤:

1) 包括最新的jquery.js和jqueryUI javascript文件。

2) 包括JQuery UI CSS文件。

3) HTML:

<div id="dialog-confirm" title="My Dialogue">
  <form id="form1" method="post">
    <div style="padding:10px;">
      Enter Text:
      <input type="text" name="legacy_code_text" id="legacy_code_text" class="textfield"/>
    </div>
  </form>
</div>

4) jQuery:

$("#dialog-confirm").dialog({
    resizable: false,
    height: 140,
    modal: true,
    buttons: {
        "Ok": function() {
            var txtarea_val = $('#form1  #legacy_code_text').val();
            if (txtarea_val != '') $('#form1').submit();
            else {
                /*show your error*/
                alert("Enter Some text..!");
                return false;
            }
        },
        Cancel: function() {
            $(this).dialog("close");
        }
    }
});