如果x <= y,那么x = 600 y = 1000?

Jay*_*rex 2 jquery

什么是正确或正确的方式?我对此感到困惑.有人能说得对吗?

3digit <= 4digitfalse
4digit <= 3digittrue

你可以在这里测试一下:JSFiddle

<h2></h2>
<form id="send" action="#" method="POST">
    <input type="text" id="x"><br/>
    <input type="text" id="y">
    <input type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
$(document).ready(function() {
    $('#send').submit(function(){
        var x = $('#x').val();
        var y = $("#y").val();
        if (x <= y) {
            $('h2').html("Done.");
        } else {
            $('h2').html("Sorry,not enough cash.");
        }
        event.preventDefault();
    });
});
Run Code Online (Sandbox Code Playgroud)

Ror*_*san 8

您需要将值转换为整数.目前你正在比较字符串.另外请注意,你需要调用preventDefault()event,在给处理函数的传递.试试这个:

$('#send').submit(function(e) { // the 'e' parameter is the event
    e.preventDefault();
    var x = parseInt($('#x').val(), 10);
    var y = parseInt($("#y").val(), 10);
    $('h2').html(x <= y ? "Done." : "Sorry, not enough cash.");
});
Run Code Online (Sandbox Code Playgroud)


Pra*_*man 5

在执行任何操作之前将其转换为整数,因为它将其作为字符串或其他内容进行排序/比较,其中"600"大于"1000":

var x = parseInt($('#x').val(), 10);
var y = parseInt($("#y").val(), 10);
Run Code Online (Sandbox Code Playgroud)

工作小提琴:https://jsfiddle.net/h60fva4p/