我正在将此 javascript 连接到服务器代码,但我无法选择正确的元素。无论我选择什么按钮,video_id 总是最终成为第一个(在本例中为“bbc”)。如何更改 javascript/jquery 以根据所选按钮为 video_id 选择正确的值?
<script type="text/javascript">
$(document).ready(function() {
$(".removebutton").submit(function(event){
event.preventDefault();
$.ajax({
type:"POST",
url:"/munch_video/",
data: {
'video_id': $('#video_id').val(), // from form
'playlist': $('.playlist').val(), // from form
'add_remove': $('.add_remove').val(), // from form
},
success: function(message){
alert(message);
$('.span8').html(message);
}
});
return false;
});
});
</script>
<form method='post' action = '/munch_video/ ' class = 'removebutton'>{% csrf_token %}
<input type="hidden" value="Channel" class = "playlist"/>
<input type="hidden" value="bbc" id = "video_id"/>
<input type="hidden" value="remove_video" class = "add_remove"/>
<input type='submit' class="btn btn-danger" value='Remove from plate'/>
</form>
<form method='post' action = '/munch_video/ ' class = 'removebutton'>{% csrf_token %}
<input type="hidden" value="Channel" class = "playlist"/>
<input type="hidden" value="toyota" id = "video_id"/>
<input type="hidden" value="remove_video" class = "add_remove"/>
<input type='submit' class="btn btn-danger" value='Remove from plate'/>
</form>
<form method='post' action = '/munch_video/ ' class = 'removebutton'>{% csrf_token %}
<input type="hidden" value="Channel" class = "playlist"/>
<input type="hidden" value="gm" id = "video_id"/>
<input type="hidden" value="remove_video" class = "add_remove"/>
<input type='submit' class="btn btn-danger" value='Remove from plate'/>
</form>
can be multiple buttons, each with a different video_id value