Javascript converts string type to int type_javascript tips
Here comes the depressing thing, first look at the front-end HTML:
Purchase Quantity:
pieces (in stock <%=GOODSNUM%>)
Use JS to get the value, pay attention to the JS code:
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:
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:
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:
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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: '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 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

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:

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.

As a strongly typed language, C++ requires special attention when converting data types, otherwise the compiler will report an error. One of the more common errors is "invalid type conversion". This article will explain why this error occurs, how to perform type conversion, and how to avoid this error. 1. Cause of the error: Data type mismatch. There are some data types in C++ that cannot be converted directly. For example, you cannot convert a character variable directly to an integer variable, or a floating-point variable directly to a Boolean variable.

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
