Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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函数?_Javascript_Html - Fatal编程技术网

当输入发生更改时,如何执行javascript函数?

当输入发生更改时,如何执行javascript函数?,javascript,html,Javascript,Html,我试图在输入更改其值时执行Javascript函数。当单击其他输入时,输入将更改其值(这些输入增加或减小值) 以下是我的html代码: <input type='button' value='–' class='qtyminus' field='quantity{{$accessory- >id}}' style="font-weight: bold;" /> <input id='accessory{{$i}}' onchange="updateNoAccesso

我试图在输入更改其值时执行Javascript函数。当单击其他输入时,输入将更改其值(这些输入增加或减小值)

以下是我的html代码:

<input type='button' value='–' class='qtyminus' field='quantity{{$accessory-
>id}}' style="font-weight: bold;" />  

<input id='accessory{{$i}}' onchange="updateNoAccessories({{$i}})" 
type='text' name='quantity{{$accessory->id}}' value='1'
class='qty' style="margin-bottom: 0px !important"/>

<input type='button' value='+' class='qtyplus' field='quantity{{$accessory-
    >id}}' style="font-weight: bold;" />
我想从JS函数更新的内容:

function updateNoAccessories(i) {
    var no = document.getElementById("accessory"+i).value;
    document.getElementById("acc"+i).innerHTML = no + ' x';
}
<span>You chose:<i><b id="acc0"></b> items,</i><i><b id="acc1"></b> 
things,</i><i><b id="acc2"></b>stuff</i></span>
您选择了:项,
东西

只需将
onchange
事件替换为
oninput
,它将在输入值发生变化时更新元素的值

因此,您的代码将如下所示:

<input type='button' value='–' class='qtyminus' field='quantity{{$accessory-
>id}}' style="font-weight: bold;" />  

<input id='accessory{{$i}}' oninput="updateNoAccessories({{$i}})" 
type='text' name='quantity{{$accessory->id}}' value='1'
class='qty' style="margin-bottom: 0px !important"/>

<input type='button' value='+' class='qtyplus' field='quantity{{$accessory-
    >id}}' style="font-weight: bold;" />


希望这能奏效

您的按钮+和-没有通过onchange链接到您的输入,因此您的事件永远不会触发(可能您没有粘贴代码的这一部分),您可以使用oninput而不是onchange

您可以通过以下方式解决此问题:

<input type='button' value='–' class='qtyminus' field='quantity{{$accessory-
>id}}' style="font-weight: bold;" onclick='updateValue(-1,$i)'/> 

<input id='accessory{{$i}}' oninput="updateNoAccessories({{$i}})" 
type='text' name='quantity{{$accessory->id}}' value='1'
class='qty' style="margin-bottom: 0px !important"/>

<input type='button' value='+' class='qtyplus' field='quantity{{$accessory-
    >id}}' style="font-weight: bold;" onclick='updateValue(+1,$i)'/>

可能的重复对我不起作用。这似乎起作用,但它将值增加2,而不是1,我不知道为什么。updateValue函数中的一个小错误是(“acc”+id),但现在当我单击输入“+”增加2时,请从updateValue函数中删除第3行,因为我的类已经增加了值。谢谢!可能是因为updateValue触发了updateNoAccessories。谢谢,我用你的编辑更新了我的答案
function updateValue(i,id) {
    var no = document.getElementById("accessory"+id).value;
    no = no + i;
    document.getElementById("acc"+id).innerHTML = no;
}

function updateNoAccessories(i) {
    var no = document.getElementById("accessory"+i).value;
    document.getElementById("acc"+i).innerHTML = no + ' x';
}