How can val() return Number?

后端 未结 5 486
梦谈多话
梦谈多话 2020-11-27 19:28

In the valdocs written this description:

.val() Returns: String, Number, Array

I tried to get a Number, b

5条回答
  •  粉色の甜心
    2020-11-27 19:58

    You could simply create a jquery plugin to use instead of val() that would do this. Here I create the function nval() to use instead of val() when fetching a elements value.

    $.fn.nval = function() {
       return Number(this.val())
    };
    

    Then in your code just use the following to get the value as a number

    $('#elementID').nval()
    

提交回复
热议问题