我的 CoffeeScript 通过这一行启动:
$(document).on 'change', '#myid1'
现在它也应该在以下变化时启动
'#myid2'
'#myid3'
'#myid4'
.
.
.
And so on..
它还应该记住启动脚本的号码。例如,如果 #myid1 发生更改,则应触发 javascript 并保存“1”。
我该怎么做?
最诚挚的问候!
最佳答案
这是您要找的吗?
代码:
$(document).on('change', '#myid1, #myid3, #myid4', function () {
var id = $(this).attr('id');
var num = id.substring(id.length - 1, id.length);
alert(num); // Should alert the correct value (1, 3, 4 etc...)
});
您可能还想考虑向每个元素添加一个类(即 <input type="text" class="inputElem" id="myid1" />
然后更改代码以处理基于此类的更改,而不是列出 Id:
$(document).on('change', '.inputElem', function () {
var id = $(this).attr('id');
var num = id.substring(id.length - 1, id.length);
alert(num); // Should alert the correct value (1, 3, 4 etc...)
});
关于javascript - 在多个 id 上触发 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25995549/