将id/类放在HTML中的正确方法是使用foreach来获得确切的值
The correct way to put the id/class in HTML using foreach to get the exact value?
我已经成功地显示了每个使用的数据。这里是代码:
<?php
foreach($jawapan as $row_jawapan):
?>
<li class="vote-li" >
<div class="round-button-circle">
<span></span>
<h1><?php echo $row_jawapan->answer_text;?></h1>
<input class="id_answer" type="hidden" value="<?php echo $row_jawapan->id_option;?>">
<input class="answerswer_text" type="hidden" value="<?php echo $row_jawapan->answer_text;?>">
</div>
</li>
<?php endforeach; ?>
现在我想得到确切的值,当用户点击它使用这个代码:
$('.vote-li').on('click', function(){
var id_question = $('.id_question').val(),
answer_text = $('.answer_text').val(),
user_id = $('.user_id').val(),
id_option = $(this).prop('id');
var data = {
id_question: id_question,
user_id: user_id,
id_option: id_option,
answer_text: answer_text
};
console.log(data);
});
现在,每当用户单击vote-li
类时,即使项目值不同,console.log
也只显示第一个索引项目。我认为我在input
元素中写入class/id
时犯了错误,但我不知道如何放置正确的id/类,以便我的jQuery能够提取正确的数据。
$('.answer_text').val()
和$('.user_id')
使用$(this).find('.id_question')
、$(this).find('.answer_text')
和$(this).find('.user_id')
,而不是$('.id_question')
,因此仅限于当前的.vote-li
。
更正代码:
$('.vote-li').on('click', function(){
var id_question = $(this).find('.id_question').val(),
answer_text = $(this).find('.answer_text').val(),
user_id = $(this).find('.user_id').val(),
id_option = $(this).prop('id');
var data = {
id_question: id_question,
user_id: user_id,
id_option: id_option,
answer_text: answer_text
};
console.log(data);
});
相关文章:
- 在JavaScript中中断forEach方法的首选方法
- 将id/类放在HTML中的正确方法是使用foreach来获得确切的值
- 不能调用未定义节点.js的方法“forEach”
- 每次迭代在 Foreach 循环中重复 PHP 睡眠方法
- 如何删除“对象在节点中没有方法'forEach'错误.js”
- 使用 forEach 方法修改 dom 元素
- JavaScript 方法 array.forEach 在 Chrome 中工作吗?
- MongoDB aggregate() - 错误“TypeError: 无法调用未定义的方法'forEach
- 不理解此forEach()方法的输出
- 使用敲除foreach绑定绑定方法名称
- 回调函数的定义以及它如何适用于array.prototype.forEach/map/filter方法
- Jquery.ech()和Array.prototype.forEach()方法的区别
- 如何在knockout.js中的foreach中使用computed方法
- 使用Array.prototype.forEach()方法
- 在Foreach循环中调用Async方法
- JavaScript: Internet Explorer不支持forEach方法
- AngularJS: forEach http get data -等待其他方法,直到所有来自循环的数据被加载
- 为什么字符串变量可以通过JS中的调用使用forEach方法
- 使用forEach方法在javascript
- 为什么ECMAScript 5中Object上没有forEach方法