成功後,我想根據存在的值顯示不同的值。例如,如果$('#add')。val()中有一個值,我想顯示'Video added'作爲成功函數的一部分。如果$('#remove')。val()在它的價值,我想顯示「視頻清除」作爲成功的部分功能。無論是#將或#remove將在給定時間有一個值,如何啓用這個?如何根據使用jQuery/Ajax的變量更改成功函數的輸出?
<script type="text/javascript">
$(document).ready(function() {
$("#test").submit(function(event){
event.preventDefault();
$.ajax({
type:"POST",
url:"/edit_favorites/",
data: {
'video_add': $('#add').val(), // from form
'video_remove': $('#remove').val() // from form
},
success: function(){
$('#message').html("<h2>Video added!</h2>")
}
});
return false;
});
});
</script>
感謝約翰。我如何解釋此聲明?我正在考慮使用if/else語句來執行此操作。 – sharataka
@sharataka沒問題。它實際上是if的短手版本。我試圖詳細說明:(布爾)?/*如果bool爲true,則執行操作* /:/ *將它填充爲false * /。 '$('#add')。val()!=='''會評估爲true或false,取決於它是否有值 – Johan