2
var enter = $('#enter').attr('value');
$('#enterButton').on("click", function() {
    if(enter == "Enter" || enter == "enter") {
        $('.loader').fadeOut(2000);
    }
});

This is my code so far, I also tried using:

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

But still nothing worked, so can anyone help me fix this? Thanks :)

EDIT: the html is:

<input id="enter" type="text" placeholder="Do what it says...">
<button class="btn" id="enterButton">Go!</button>

EDIT FINAL:

The final answer is:

$('#enterButton').on("click", function() {
    var enter = $('#enter').val();
    if(enter == "Enter" || enter == "enter") {
        $('.loader').fadeOut(2000);
    }
});

Because, the variable was outside, and was not updating with the button click :) Thanks @dystroy

4

3 回答 3

7

您不会更新enter单击时的值,因此您只测试初始值,当然,它不会改变。

用这个 :

$('#enterButton').on("click", function() {
    var enter = $('#enter').val();
    if(enter == "Enter" || enter == "enter") {
        $('.loader').fadeOut(2000);
    }
});
于 2013-07-21T14:22:30.937 回答
1

您也可以使用它。

$('#enterButton').on("click", function() {
var enter = $('#enter').val();
    enter = enter.toLowerCase();
    if(enter == "enter") {
        $('.loader').fadeOut(2000);
    }
});
于 2013-07-21T14:26:56.157 回答
0

试试这个代码:

var enter = $('#enter').val();
$(document).on("click", "#enterButton", function() {
    if(enter == "Enter" || enter == "enter") {
        $('.loader').fadeOut(2000);
    }
});
于 2013-07-21T14:21:07.633 回答