[英]How do I change the output of the success function based on a variable using jQuery/Ajax?
On success, I would like to show different values depending on what value exists. 成功之后,我想根据存在的值显示不同的值。 For example, if $('#add').val() has a value in it I would like to show 'Video added" as part of the success function. If $('#remove').val() has a value in it, I would like to show 'Video removed' as part of the success function. Either #add or #remove will have a value in it at a given time. How do I enable this? 例如,如果$('#add')。val()中有一个值,我想在成功函数中显示“已添加视频”。如果$('#remove')。val()中有一个值中,我想在成功功能中显示“已删除视频”。在给定的时间#add或#remove都将具有值。如何启用此功能?
<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>
如果确定只有一个文本字段具有值,则可以执行以下操作:
$('#message').html('<h2>' + $('#add').val() !== '' ? 'Video added' : 'Video removed' + '!</h2>' )
if just checking text is enough you can use this code: 如果仅检查文本就可以使用以下代码:
<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(){
if ( $('#add').text() ) {
$('#message').html("<h2>Video added!</h2>");
} else if ( $('#add').text() ){
$('#message').html("<h2>Video removed!</h2>");
} else {
$('#message').html("<h2>Wasn't either add or remove!</h2>");
}
}
});
return false;
});
});
</script>
I assumed #add
or #remove
are text. 我假设#add
或#remove
是文本。 The key is this if
check: 关键是if
检查以下内容:
if ( $('#add').text() ) {
//... add is not empty because there is some text in it...
If #add
or #remove
may contain text or any other element, you can check them with :empty
selector: 如果#add
或#remove
可能包含文本或任何其他元素,则可以使用:empty
选择器检查它们:
if ( !$('#add:empty').length ) {
//... #add is not empty so something was added successfully ...
If #add
or #remove
are <input>
elements like text box you can do the same check with: 如果#add
或#remove
是<input>
元素(例如文本框),则可以使用以下方法进行相同的检查:
if ( $('#add').val() ) {
//... add is not empty ...
I like to return a response when the backend script has executed, XML would look something like this 我想在后端脚本执行后返回响应,XML看起来像这样
<status>true</status>
<successMessage>Video deleted</successMessage>
or 要么
<status>false</status>
<errorMessage>Video not found on the server</errorMessage>
Javascript 使用Javascript
success: function (response) {
if($(response).find("status").text()=="true"){
$(".success").html($(response).find("successMessage").text());
$(".success").show();
}else{
$(".error").html($(response).find("errorMessage").text());
$(".error").show();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.