Javascript converts string type to int type

Source: Internet
Author: User

The depressing thing is coming. first look at the front-end HTML: CopyCode The Code is as follows: purchase quantity: <input id = "txtnum" type = "text" value = "1" runat = "server" onchange = "javascript: checknum ();"/>
Parts (Inventory <span id = "getgoodsnum"> <% = goodsnum %> </span>)

Use JS values. Pay attention to the JS Code:Copy codeThe Code is as follows: var num = Document. getelementbyid ("txtnum"). value;
VaR goodsnum = Document. getelementbyid ("getgoodsnum"). innerhtml;

You will find that the txtnum and getgoodsnum values are different methods.
Txtnum uses. value, and getgoodsnum uses. innerhtml.
Because getgoodsnum is the span label used, txtnum is the text box.
SPAN, table, and Div have no value, so the innerhtml value is used.
Txtnum belongs to the text box, lable, and drop-down boxes all have values ).
Now everyone understands.
Now let's talk about the comparison of two numbers. You must think that the two numbers are now available.
See JS Code:Copy codeThe Code is as follows: if (Num> goodsnum ){
Alert ("the number of shopping items cannot exceed the inventory quantity! ");
Return false;
}

In this case, there should be no problem. We will compare the two numbers, and then we will import and compare the data. If num is set to 100 and goodsnum is set to 90, verification is normal. Then, input 90 for num and 100 for goodsnum. Verify that there is a problem and the prompt "shopping Quantity cannot exceed inventory quantity!" is displayed. What is the problem. Then use alert to output the two parameters. By the way, are the two numbers of the string type. Why did you forget it? The brain is short. Convert it.
There are two methods available: One:Copy codeThe Code is as follows: if (Num/1)> (goodsnum/1 )){
Alert ("the number of shopping items cannot exceed the inventory quantity! ");
Return false;
}

In this way, it is OK to remove 1, and it is difficult to verify.
Two:Copy codeThe Code is as follows: if (parseint (Num)> parseint (goodsnum )){
Alert ("the number of shopping items cannot exceed the inventory quantity! ");
Return false;
}

Verify OK, pass, and solve the problem.
Author: Mr S. R Lee
Source: http://www.cnblogs.com/LeeYongze

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.