一个页面中有多个input输入框,我给每个input输入框加了一个onChange事件。我想要在输入框触发onChange事件的时候,获取到这些框的id或者是name,然后将他们放到一个字符串数组里。
<input type="text" id="userName" name="userName" onchange="getValue(this)">
<input type="text" id="phone" name="phone" onchange="getValue(this)">
<input type="text" id="email" name="email" onchange="getValue(this)">
<input type="text" id="city" name="city" onchange="getValue(this)">
<script>
function getValue(event){
console.log(event.id, event.name, event.value)
}
</script>
可以利用事件委托,不用多次绑定事件
<div id="box">
<input type="text" id="aaa" name="aaaname"/>
<input type="text" id="bbb" name="bbbname"/>
<input type="text" id="ccc" name="cccname"/>
<div>
<script>
document.getElementById('box').addEventListener('change',function(e){
console.log("id: " + e.target.id,"name: " + e.target.name,"value: " + e.target.value)
})
</script>