jQuery val() comparison

前端 未结 3 1024
执念已碎
执念已碎 2021-01-22 00:21

I am trying to compare the values of two input fields with jQuery. However, it does seem correct but it doesn\'t alert when one of the values is larger.

<         


        
3条回答
  •  粉色の甜心
    2021-01-22 01:23

    You're comparing strings, not integers. You can use parseInt() to convert them. Try this:

    if (parseInt($('#charsLeft').val(), 10) < parseInt($('#charsLeft2').val(), 10)) {
        alert('Key is shorter than the plaintext');
    }
    

    Updated fiddle

提交回复
热议问题