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