2
<form action="index.php" method="POST" id="addcredits">
    <input type="text" id="name" placeholder="Username"><br /><br />
    <input type="text" id="amount" placeholder="credits amount"><br /><br />
    <input type="submit" id="submit">
</form>

$(document).ready(function() {
    $("#addcredits").submit(function(event) {
        event.preventDefault();
        var username = $("#name");
        var amount = $("#amount");
        var submit = $("#amount");
        var error = $("#error");

        if (username.val() != null && amount.val() != null) {
            console.log("hi");
        } else {

            error.html("One of the fields were empty..");
            error.fadeIn("fast");

            setTimeout(function() {
                error.fadeOut("fast");
            }, 5000);
        }

    });
});

由于占位符文本,用户名和金额字段始终不为空。有没有办法让 jquery 忽略占位符?

4

6 回答 6

4

val()总是返回一个字符串,所以检查空字符串。jQuery 有一个delay()动画方法,所以不需要超时,如果你只使用一次选择器,缓存它们是没有意义的:

$(document).ready(function() {
    $("#addcredits").om('submit', function(event) {
        event.preventDefault();

        if ( $("#name").val().length && $("#amount").val().length ) {
            console.log("hi");
        } else {
            $("#error").html("One of the fields were empty..");
                       .fadeIn("fast");
                       .delay(5000)
                       .fadeOut("fast");
        }
    });
});
于 2013-06-22T15:22:43.960 回答
2

尽管您的大多数答案都是正确的,但我认为@Jony Kale 提出的问题是另一回事。他总是得到占位符值。为避免检查输入 html 元素中的占位符值,请尝试以下操作:

(element.val() != $(element).attr("placeholder")

然后,您可以在检查输入是否为空的函数中使用它:

function isEmpty(element) {
    return ((element.val() != $(element).attr("placeholder")) && (element.val().length > 0));
}
于 2015-03-24T22:00:12.640 回答
1

不要将其与 null 进行比较,而是尝试比较它是否为空字符串

username.val().length;

或者

username.val() != "";
于 2013-06-22T15:16:32.070 回答
0

试试这个:- http://jsfiddle.net/aiioo7/5nkG3/1/

JS:-

$(document).ready(function () {
    $("#addcredits").submit(function (event) {
        event.preventDefault();
        var username = $("#name");
        var amount = $("#amount");
        var submit = $("#amount");
        var error = $("#error");

        if (username.val().length > 0 && amount.val().length > 0) {
            console.log("hi");
        } else {

            error.html("One of the fields were empty..");
            error.fadeIn("fast");

            setTimeout(function () {
                error.fadeOut("fast");
            }, 5000);
        }

    });
});

HTML:-

<form action="index.php" method="POST" id="addcredits">
    <input type="text" id="name" placeholder="Username">
    <br />
    <br />
    <input type="text" id="amount" placeholder="credits amount">
    <br />
    <br />
    <input type="submit" id="submit">
    <div id="error"></div>
</form>
于 2013-06-22T15:19:53.437 回答
0
if (username.val().length!=0 && amount.val().length != 0) {
    console.log("hi");
} else {
    console.log("working");
}
于 2013-06-22T15:21:30.287 回答
0

你不应该用 来检查值null,这里不会是nullblank,所以username.val()检查就足够了。你placeholder可以使用attrlike来检查它,

username.val()!=username.attr('placeholder')

完整代码

 if (username.val()  && username.val()!=username.attr('placeholder')
  && amount.val()  && amount.val()!=amount.attr('placeholder')) {
        console.log("hi");
 } 
于 2013-06-22T15:22:23.323 回答