开发者

How to clear textfields after a ajax query using jquery?

开发者 https://www.devze.com 2023-04-06 16:01 出处:网络
im using this jquery function to do a ajax query: $(\"#add\").click(function() { val1 = $(\'#add_lang_level\').val();

im using this jquery function to do a ajax query:

$("#add").click(function() {    
   val1 = $('#add_lang_level').val();
   val = $('#add_lang').val();
   listitem_html = '<li>';
   listitem_html += '<span id="lang_val">' + val + '</span> <strong>('+ val1 + '/100)</strong>';
   $.ajax({ url: 'addremovelive.开发者_运维技巧php', data: {addname: val,addlevel: val1}, type: 'post'});
   listitem_html += '<a href="#" class="remove_lang"> <span class="label important">Remove</span></a>'
   listitem_html += '</li>';
    $('#langs').append(listitem_html);
});

I want to clear the content of some textfields after ajax query is complete, but dont know how. Thanks for any help!


$("#add").click(function() {    
   val1 = $('#add_lang_level').val();
   val = $('#add_lang').val();
   listitem_html = '<li>';
   listitem_html += '<span id="lang_val">' + val + '</span> <strong>('+ val1 + '/100)</strong>';
   $.ajax({ url: 'addremovelive.php', data: {addname: val,addlevel: val1}, type: 'post',
   success: function(d) {
     $('#add_lang_level').val('');
     $('#add_lang').val('');
   }
});
   listitem_html += '<a href="#" class="remove_lang"> <span class="label important">Remove</span></a>'
   listitem_html += '</li>';
    $('#langs').append(listitem_html);
});

you can do it this way, I have added anonymus function which will be invoked on success.


You can supply a success callback function as an argument to the ajax function. The callback will execute upon successful completion of the asynchronous call:

$.ajax({ 
    url: 'addremovelive.php', 
    data: {addname: val,addlevel: val1}, 
    type: 'post',
    success: function() {
        //Do whatever you need to...
        $("#yourTextField").val("");
    }
});

Note that if that is all you are doing with your ajax call, you could also use the shorthand post method:

$.post("addremovelive.php", { 
    addname: val, addlevel:val1 
}, 
function() { 
    //Callback 
});

Check the docs for both the ajax and post methods to get an idea of the options available to each.


Try adding a success callback and do your clearing in there:

$.ajax({ 
   url: 'addremovelive.php',
   data: {addname: val,addlevel: val1},
   type: 'post',
   success: function(){
      //this function is a callback that is called when the AJAX completes
   }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号