我的div与class =“Vote”是
<div class="Vote">
<input type="hidden" name="q_id" class="q_id" id="q_id" q_id="{{ result.object.id }}" value="{{ result.object.id }}">
<button type="submit" class="downVote" value="downVote">downVote</button>
在我的html页面上有几个这种类型的div,
我用jquery调用ajax是
$('.downVote').click(function() {
var q_id=$(this).attr('q_id')
console.log(q_id)
$.ajax( {
type: 'GET',
url:"http://127.0.0.1:8000/q/downVote/",
data:q_id,
success: searchSuccessDown,
dataType: 'html'
});
});
function searchSuccessDown(data) {
console.log('downVoted successfully');}
我是新手,我的问题是当点击一个downVote按钮时(页面上有几个downVote按钮)如何选择id =“q_id”或class =“q_id”的输入标签为class =“Vote”的相应div并通过ajax数据传递其值.
解决方法:
一种方法是获取父元素(这是投票div),然后在其中找到q_id元素:
var q_id = $(this).parent().find('.q_id').val();
这是一个快速的小提琴:
https://jsfiddle.net/1m56g6jL/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。