【问题标题】:How to check if textbox is empty in jquery dialog如何在jquery对话框中检查文本框是否为空
【发布时间】:2013-05-22 07:46:02
【问题描述】:

我的应用中有一个 jquery 对话框。

这就是它的制作方法

    var routePopup = "Route Name : <input type='text' id='routeName' name='routeName'/>";


    document.getElementById('popUp').innerHTML = "";
    document.getElementById('popUp').innerHTML = routePopup;

    $("#popUp").dialog({
        title: "Route Name",
        resizable: false,
        width: auto,
        height: auto,
        buttons: {
            "Create Route": function () {
                $(this).dialog("close");
            }
        }
    });

现在我想检查文本框是否为空。如果它是空的,那么它不应该允许关闭弹出窗口。

还应该在顶部给出消息,该值不能为空。

【问题讨论】:

标签: javascript jquery


【解决方案1】:

如果你可以写$('#popUp').html('');,你为什么要写document.getElementById('popUp').innerHTML = ""; - 这就是你使用jQuery的原因:)

$("#popUp").dialog({
        title: "Route Name",
        resizable: false,
        width: auto,
        height: auto,
        buttons: {
            "Create Route": function () {
                if ($(this).val() == '') {
                    alert('May not be empty!');
                }
                else {
                    $(this).dialog("close");
                }
            }
        }
    });

【讨论】:

  • 如果文本框为空,我也想在顶部向用户发送消息。
  • 查看更新的答案。它使用 alert() 但你可以做任何其他事情
【解决方案2】:
var val = $('#textbox').val();

if (val) {
    //Do whatever...
}

【讨论】:

    【解决方案3】:

    在检查长度之前修剪输入值:

    if ( $("#txt").val().trim().length == 0 )
    {
        // do something
    }
    

    【讨论】:

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