如何在jquery对话框中检查文本框是否为空

时间:2013-05-22 07:46:02

标签: javascript jquery

我的应用程序中有一个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");
            }
        }
    });

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

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

3 个答案:

答案 0 :(得分:2)

如果你可以document.getElementById('popUp').innerHTML = "";,你为什么写$('#popUp').html(''); - 这就是你使用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");
                }
            }
        }
    });

答案 1 :(得分:0)

var val = $('#textbox').val();

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

答案 2 :(得分:0)

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

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