Home Web Front-end JS Tutorial Javascript converts string type to int type_javascript tips

Javascript converts string type to int type_javascript tips

May 16, 2016 pm 06:14 PM
int string type conversion

Here comes the depressing thing, first look at the front-end HTML:

Copy code The code is as follows:

Purchase Quantity:
pieces (in stock <%=GOODSNUM%>)

Use JS to get the value, pay attention to the JS code:
Copy code The code is as follows:

var num = document.getElementById("txtNum").value;
var goodsnum = document.getElementById( "getGoodsNum").innerHTML;

You will find that the value methods of txtNum and getGoodsNum are different.
txtNum uses .value, getGoodsNum uses .innerHTML.
Because getGoodsNum uses the span tag and txtNum is the text box.
span, table, and div have no value, so innerHTML is used to obtain the value.
txtNum belongs to the text box, label, and drop-down box all have value.
Now everyone understands.
Now let’s compare the two numbers. Everyone must be thinking that now we have obtained these two numbers.
Please see the JS code:
Copy code The code is as follows:

if (num > ; goodsnum) {
alert("The shopping quantity cannot be greater than the inventory quantity!");
return false;
}

Looking at it this way, there should be no problem. Comparing the two numbers, Then I input the data and compare. Enter num as 100 and goodsnum is 90. Verify that it is normal. Then num loses 90 and goodsnum loses 100. Verification, something went wrong, it prompted "The purchase quantity cannot be greater than the inventory quantity!" 》. What's going on? Then use alert to output the two parameters, yes, and then think about it. By the way, are these two numbers of string type? How could I forget? My brain is short-circuited. Convert it.
Two methods are now provided, One:
Copy code The code is as follows:

if ((num / 1) > (goodsnum / 1)) {
alert("The purchase quantity cannot be greater than the inventory quantity!");
return false;
}

In this way, it is OK to remove 1, but it is difficult to verify.
Two:
Copy code The code is as follows:

if (parseInt(num) > ; parseInt(goodsnum)) {
alert("The shopping quantity cannot be greater than the inventory quantity!");
return false;
}

Verification OK, passed, solved.
Author: Mr S.R Lee
Source: http://www.cnblogs.com/LeeYongze
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

How to solve C++ runtime error: 'invalid type conversion'? How to solve C++ runtime error: 'invalid type conversion'? Aug 27, 2023 pm 03:33 PM

How to solve C++ runtime error: 'invalidtypeconversion'? During the C++ programming process, we often encounter various compile-time and run-time errors. One of the common runtime errors is the 'invalidtypeconversion' error. This error is triggered when we convert one data type to another incompatible data type. This article will introduce some common causes of this error and how to solve it.

Detailed explanation of the method of converting int type to bytes in PHP Detailed explanation of the method of converting int type to bytes in PHP Mar 06, 2024 pm 06:18 PM

Detailed explanation of the method of converting int type to byte in PHP In PHP, we often need to convert the integer type (int) to the byte (Byte) type, such as when dealing with network data transmission, file processing, or encryption algorithms. This article will introduce in detail how to convert the int type to the byte type and provide specific code examples. 1. The relationship between int type and byte In the computer field, the basic data type int represents an integer, while byte (Byte) is a computer storage unit, usually 8-bit binary data

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

2w words detailed explanation String, yyds 2w words detailed explanation String, yyds Aug 24, 2023 pm 03:56 PM

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

Type conversion of golang function Type conversion of golang function Apr 19, 2024 pm 05:33 PM

In-function type conversion allows data of one type to be converted to another type, thereby extending the functionality of the function. Use syntax: type_name:=variable.(type). For example, you can use the strconv.Atoi function to convert a string to a number and handle errors if the conversion fails.

C++ program to convert double type variable to int type C++ program to convert double type variable to int type Aug 25, 2023 pm 08:25 PM

In C++, variables of type int can only hold positive or negative integer values; they cannot hold decimal values. There are float and double values ​​available for this purpose. The double data type was created to store decimals up to seven digits after the decimal point. Conversion of an integer to a double data type can be done automatically by the compiler (called an "implicit" conversion), or it can be explicitly requested by the programmer from the compiler (called an "explicit" conversion). In the following sections, we'll cover various conversion methods. Implicit conversions The compiler performs implicit type conversions automatically. To achieve this, two variables are required - one of floating point type and the other of integer type. When we simply assign a floating point value or variable to an integer variable, the compiler takes care of all the other things

See all articles