Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中单击以减去1_Javascript_Input - Fatal编程技术网

在javascript中单击以减去1

在javascript中单击以减去1,javascript,input,Javascript,Input,我想要一些东西,比如10个按钮,每次我按下它应该减去1。到了零,它应该消失了。 html代码 <table width="600" border="0" cellspacing="0" cellpadding="5"> <tr> <th scope="col"><input type="button" onclick="add()" id='mynum9' value="9" ></th> <th scope=

我想要一些东西,比如10个按钮,每次我按下它应该减去1。到了零,它应该消失了。 html代码

<table width="600" border="0" cellspacing="0" cellpadding="5">
  <tr>
    <th scope="col"><input type="button" onclick="add()" id='mynum9' value="9" ></th>
    <th scope="col"><input type="button" onclick="add()" id='mynum7' value="7" ></th>
    <th scope="col"><input type="button" onclick="add()" id='mynum4' value="4" ></th>
    <th scope="col"><input type="button" onclick="add()" id='mynum5' value="5" ></th>
    <th scope="col"><input type="button" onclick="add()" id='mynum1' value="1" ></th>
  </tr>
  <tr>
    <td><input type="button" onclick="add()" id='mynum3' value="3" ></td>
    <td><input type="button" onclick="add()" id='mynum10' value="10" ></td>
    <td><input type="button" onclick="add()" id='mynum8' value="8" ></td>
    <td><input type="button" onclick="add()" id='mynum2' value="2" ></td>
    <td><input type="button" onclick="add()" id='mynum6' value="6" ></td>
  </tr>
</table>


function add() {
    var numArray=["mynum1". "mynum2","mynum3". "mynum4","mynum5". "mynum6","mynum7". "mynum8","mynum9". "mynum10"]
    for (var i = 0; i < numArray.length; i++);
    var num = document.getElementById(numArray[i]).value;
    if(num == '') num = 0;
if(num==1){
        $(this).remove();
    }
    document.getElementById(numArray[i]).value = parseInt(num ,10) - 1;
    }

函数add(){
var numaray=[“mynum1”、“mynum2”、“mynum3”、“mynum4”、“mynum5”、“mynum6”、“mynum7”、“mynum8”、“mynum9”、“mynum10”]
对于(var i=0;i
但它不起作用,我做错了什么。另外,如果您能告诉我最佳javascript实践网站,请尝试以下方法:

<table width="600" border="0" cellspacing="0" cellpadding="5">
  <tr>
    <th scope="col"><input type="button" onclick="add()" id='mynum9' value="9" ></th>
    <th scope="col"><input type="button" onclick="add()" id='mynum7' value="7" ></th>
    <th scope="col"><input type="button" onclick="add()" id='mynum4' value="4" ></th>
    <th scope="col"><input type="button" onclick="add()" id='mynum5' value="5" ></th>
    <th scope="col"><input type="button" onclick="add()" id='mynum1' value="1" ></th>
  </tr>
  <tr>
    <td><input type="button" onclick="add()" id='mynum3' value="3" ></td>
    <td><input type="button" onclick="add()" id='mynum10' value="10" ></td>
    <td><input type="button" onclick="add()" id='mynum8' value="8" ></td>
    <td><input type="button" onclick="add()" id='mynum2' value="2" ></td>
    <td><input type="button" onclick="add()" id='mynum6' value="6" ></td>
  </tr>
</table>
<script>
var numArray=["mynum1", "mynum2","mynum3", "mynum4","mynum5", "mynum6","mynum7", "mynum8","mynum9", "mynum10"];
function add() {

    for (var i = 0; i < numArray.length; i++){
    var num = document.getElementById(numArray[i]).value;

if(num == 1){
    document.getElementById(numArray[i]).remove();
    removeA(numArray, numArray[i]);
    if(numArray.length > 0){
    document.getElementById(numArray[i]).value = parseInt(num ,10);
    }
     continue;
    }
document.getElementById(numArray[i]).value = parseInt(num ,10) - 1;
    }
}

function removeA(arr) {
    var what, a = arguments, L = a.length, ax;
    while (L > 1 && arr.length) {
        what = a[--L];
        while ((ax= arr.indexOf(what)) !== -1) {
            arr.splice(ax, 1);
        }
    }
    return arr;
}



</script>

变量numaray=[“mynum1”、“mynum2”、“mynum3”、“mynum4”、“mynum5”、“mynum6”、“mynum7”、“mynum8”、“mynum9”、“mynum10”];
函数add(){
对于(var i=0;i0){
getElementById(numArray[i]).value=parseInt(num,10);
}
持续
}
getElementById(numaray[i]).value=parseInt(num,10)-1;
}
}
功能移除(arr){
var what,a=参数,L=a.length,ax;
while(长度>1&&arr.length){
什么=a[--L];
while((ax=arr.indexOf(what))!=-1){
阵列拼接(ax,1);
}
}
返回arr;
}

希望有帮助。

您能定义“不工作”吗?您到底有什么问题?这是语法错误吗?请更具体地说明你需要什么帮助。就目前的情况而言,你的帖子看起来就像是在说:“为我修复我的代码”。你有一个
for
循环,它什么都不做:
for(…),数组中的点而不是逗号,等等。您是否试图在单击一个按钮时减少所有按钮的值?另外,JS是否在
?非常感谢您的支持