答案 0 :(得分:1)
这里的要点不仅仅是使用PHP array
值作为您的潜在事件目标标识符,它通常是关于使用动态生成的数据作为元素的一部分' id
个属性。在这里,您最好在JS中定位class
而不是id
。因此,当事件触发时,您检查目标的id
值。
$(document).on('click', '.i-am-target-class', function () {
var targetId = $(this).attr('id');
// do your stuff knowing id
});
你想把你的PHP代码作为你的事件目标传递给你的东西远远不是市场上最好的解决方案
<强> UPD:强>
PHP:
<select id="<?php echo $userArray[$key]['userID']; ?>" class="value-to-save"></select>
JS:
var results = {};
$('#save-button').click(function () {
$('.value-to-save').each(function () {
var userId = $(this).attr('id');
var value = $(this).val();
results[userId] = value;
});
});
因此,最终您将获得包含所有数据的results
对象