简体   繁体   English

如何使用jQuery / Ajax基于变量更改成功函数的输出?

[英]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.

相关问题 jquery ajax 调用成功,如何更改包装器 javascript 函数中的全局变量? - jquery ajax call success, how do I change a global variable in the wrapper javascript function? 我到底如何编写一个回调函数来更改jquery的AJAX中的全局变量? - How exactly do I write a callback function to change a global variable inside AJAX for jquery? 如何在“ jQuery.ajax({success:function(data)””中设置类似“ data”的回调函数参数? - How do I set a callback function parameter like 'data' in the “jQuery.ajax({ success : function(data)”? 如何调试Jquery Success函数 - How do I debug Jquery Success function 我在Ajax成功回调中将jQuery .fade()函数放在哪里? - where do i put jQuery .fade() function in ajax success callback? 将命名函数用作jQuery ajax成功回调时需要括号吗? - do I need parenthesis when using a named function as an jquery ajax success callback 我如何在ajax成功函数中为实例变量赋值 - How do i assign value to instance variable within ajax success function 如何在jquery ajax成功函数中引用调用dom元素? - How do I reference the calling dom element in a jquery ajax success function? 全局变量在JQuery Ajax成功函数中不受影响 - Global Variable not affected in JQuery Ajax Success Function 如何从成功中获取ajax响应并使用jQuery将其分配给变量? - How to get the ajax response from success and assign it in a variable using jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM