将+1添加到只读文本输入字段的jQuery值 [英] Adding +1 to jQuery value of a readonly text input field

查看:65
本文介绍了将+1添加到只读文本输入字段的jQuery值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个函数,我有 readonly text 输入,当我执行我想要的函数时数字值+ 1.所以假设我有60,当我执行该函数时,返回的数字应该是61.

I am using a function where I have a readonly text input, and when I execute the function I want the number value + 1. So let's say I have 60, when I execute the function, the number returned should be 61.

但是它出现了601,这是只需将数字1添加到字符串中。关于发生了什么的任何线索?减法,乘法和除法都可以正常工作。这是一个片段

But instead it's coming out 601, which is just adding the number 1 to the string. Any clue as to what is going on? Subtraction, multiplication and division all work fine. Here is a snippet

 var num= $("#originalnum").val() + 1;
 $("#originalnum").val(num);

是的,我尝试了一些不同的变体,我错过了什么?

And yes i've tried a few different variations, am I missing something?

推荐答案

在这种情况下,简单的一元 + 足以将字符串转换为数字:

A simple unary + is sufficient to turn a string into a number in this case:

var num = +$("#originalnum").val() + 1;
$("#originalnum").val(num);

这篇关于将+1添加到只读文本输入字段的jQuery值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆