gpt4 book ai didi

javascript - 捕获一系列 <input> 元素上的按键事件并对值求和

转载 作者:行者123 更新时间:2023-11-30 13:25:30 25 4
gpt4 key购买 nike

我有一系列输入文本元素,其名称类似于 p_at[0][int]、p_at[1][int]、...。我需要为页面中的每个 input 捕获一个 onKeypress 事件以求和。请注意,输入是动态生成的。我尝试以多种方式解决它,但对我没有任何帮助。

<input type="text"  value="1" name="p_at[0][65]">
<input type="text" value="1" name="p_at[0][66]">
<input type="text" value="1" name="p_at[0][67]">
<input type="text" value="1" name="p_at[0][68]">
<input type="text" value="1" name="p_at[0][69]">
<input type="text" value="0" name="sum[0]">
<input type="text" value="1" name="p_at[1][65]">
<input type="text" value="1" name="p_at[1][66]">
<input type="text" value="1" name="p_at[1][67]">
<input type="text" value="1" name="p_at[1][68]">
<input type="text" value="1" name="p_at[1][69]">
<input type="text" value="0" name="sum[1]">

最佳答案

没有 jQuery 答案

var inputlist = document.getElementsByTagName('input');

var inputListener = function (evt) {
//put the code that handles the event here
};

for( var i = 0; i < inputlist ){
if( inputlist[i].type === "text" && inputlist.name.substr(0,4) === "p_at" ){
if (elem.addEventListener) // W3C DOM
elem.addEventListener("Keypress",inputListener ,false);
else if (elem.attachEvent) { // IE DOM
elem.attachEvent("onKeypress", inputListener );
}
}
}

使用 jQuery Answer

$('input[type="text"][name^="p_at"]').keypress(function(evt){
//put the code that handles the event here
});

关于javascript - 捕获一系列 &lt;input&gt; 元素上的按键事件并对值求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8618961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com