我將此javascript連接到服務器代碼,但我無法選擇正確的元素。無論我選擇哪個按鈕,video_id總是成爲第一個(本例中爲「bbc」)。如何根據所選按鈕更改javascript/jquery以選擇video_id的正確值?如何選擇使用javascript的正確元素?
<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
ID必須是唯一做的發現。這不起作用。 – Ohgodwhy
我現在在代碼中將video_id更改爲class而不是id。 – sharataka