如何使函数递减以正常工作 [英] How do I get the function to de-increment to work correctly

查看:105
本文介绍了如何使函数递减以正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< script> 
var i = 0 ;
var qty1 = document .getElementById(' qty1')。value;
function buttonAdd1(){
document .getElementById(' qty1')。value = ++ i;
}
function buttonSubtract1(){
if (qty1> ; 0 ){
document .getElementById( ' qty1')。value = --i;}
}
< / script>

< table>< tbody>< tr>< td>< form action = cart.php method = get>
< input type = button onclick = buttonSubtract1() name = subtract1 value = - />
< input type = text size = 4 id = qty1 name = quantity1 value = 0 />
< input type = button onclick = buttonAdd1() name = add1 value = + />
< input type = submit name = product1 value = 添加 />
< / 表格 > < / td > < / tr > < / tbody > < / 表格 >





它递增和递增就好了所以我想这样做它不会减去负值所以我为qty1添加了一个var设置要求if(qty1> 0)然后它可以减少但不再减去任何值。我该如何解决这个问题?

解决方案

你的代码中有很多错误:

1.错误的顺序:你放了这个在呈现qty1文本框之前,qty1变量将是未定义的

 var qty1 = document.getElementById('qty1')。value; 



2.您只增加文本框中的值,而不是qty1变量,因此qty1变量保持未定义

 if(qty1> 0){



3.您已将字符串从textbox解析为整数以进行后续算术运算:

 var qty1 = parseInt(


qty1.value);



查看我的解决方案:

 <  !DOCTYPE     html  >  
< html >
< 正文 >
< 表格 action = cart.php 方法 < span class =code-keyword> = get >
< 输入 type = button onclick = bu ttonSubtract1() name = subtract1 value = - / >
< 输入 类型 = text size = 4 id = qty1 名称 = quantity1 value = 0 / >
< 输入 type = 按钮 onclick = buttonAdd1() 名称 = add1 value = + / >
< span class =code-keyword>< input type = submit 名称 = product1 value = 添加 / >
< / form >
< script >
var

qty1 = document.getElementById('qty1')
var i = 0;
var qty1 = parseInt(


<script>
    var i = 0;
	var qty1 =  document.getElementById('qty1').value;
	function buttonAdd1() {
        document.getElementById('qty1').value = ++i;
    }
	function buttonSubtract1() {
	if (qty1 > 0) {
        document.getElementById('qty1').value = --i;}
    }
</script>

<table><tbody><tr><td><form action="cart.php" method="get">
		 <input type="button" onclick="buttonSubtract1()" name="subtract1" value="-"/>
		 <input type="text" size="4" id="qty1" name="quantity1" value="0"/>
		 <input type="button" onclick="buttonAdd1()" name="add1" value="+"/>
         <input type="submit" name="product1" value="Add"/>
       </form></td></tr></tbody></table>



It incremented and de-incremented just fine so I wanted to make it so that it wont de-increment into a negative value so I added a var for qty1 and set the requirement if(qty1>0) then it can de-increment but it is no longer de-incrementing any value. How do I fix this?

解决方案

There are a number of mistakes in your code:
1. Wrong sequence: you have placed this before the qty1 textbox is rendered, qty1 variable will be undefined

var qty1 = document.getElementById('qty1').value;


2. You only increase the value in the textbox, not the qty1 variable, so the qty1 variable remains undefined

if (qty1 > 0) {


3. You have parse the string from textbox to integer for subsequent arithmetic operation:

var qty1 = parseInt(


qty1.value);


See my solution:

<!DOCTYPE html>
<html>
<body>
<form action="cart.php" method="get">
<input type="button" onclick="buttonSubtract1()" name="subtract1" value="-"/>
<input type="text" size="4" id="qty1" name="quantity1" value="0"/>
<input type="button" onclick="buttonAdd1()" name="add1" value="+"/>
<input type="submit" name="product1" value="Add"/>
</form>
<script>
var


qty1 = document.getElementById('qty1') var i = 0; var qty1 = parseInt(


这篇关于如何使函数递减以正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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