Just clear the field I want

Asked

Viewed 74 times

1

have this script:

$(".btn_contact").click(function () {

    $.ajax({
        type: "POST",
        url: "./Inserir16",
        data: $("#feedback_form16").serialize(), // serializes the form's elements.
        dataType: "json",
        success: function (data)
        {
            $(".success_messages").removeClass('hide'); // success message
        }, 
        error: function(data){
            $(".error_message").removeClass('hide'); // error message
        },
        complete: function()
        { 
            $("#feedback_form16").find('input').val(''); //clear text
        }           
    });

});

This way eliminates all input fields, but I just want to delete a specific field of the form, which has the name and id of Qtd. Anyone can help?

  • Have you tried $("#Qtd").val('')?

  • @dvd, I tried but it didn’t work

  • Okay! But you put in which function?

  • In the complete function

3 answers

2

It’s pretty simple, if you’re using jQuery, just use this code:

$("input[name='nome-do-input']").val('');

Or, if there are inputs with the name you want in places other than the form you want to clear, you can follow the line of reasoning of your current code like this:

$("#feedback_form16").find('input[name="nome-do-input"]').val(''); //clear text
  • this way inserts in it in the table, but does not clear the input

  • This function does clean the input, for sure. Now the problem may be that in your function, you do not have the input generated on your page at the time you try to clean it, OR you are populating the input data after cleaning it. To remove this doubt, throw this code: $("input[name='nome-do-input']").val(''); in your browser’s Developer Tools Console and see that the input will be clean.

2

Hello,

You currently clear all form fields in complete Function() with this code:

$("#feedback_form16").find('input').val(''); //clear text

So you need to clear only the quantity input, right? Add an id to the desired input, like this:

<input type="text" name="quantidade" id="quantidade" />

And in complete Function() refer to it this way:

$("#quantidade").val(''); //clear text
  • I applied the racicionio that indicated but did not eliminate the value in the quantity input

  • Try putting that stretch $("#quantity"). val(''); /clear text outside the complete Function, inside the structure of the click Function, but outside the ajax grouping, pq it seems that is not happening the success of the function disposed in the url defined in your ajax "insert 16"

  • But if put to clean all, success occurs and cleans all inputs

  • Put the html input here, to see how it is

  • tries to put the $("#amount"). val('); inside the Success Function()

1


Probably your HTML has ids duplicated and using #Qtd is not finding the right element. An HTML page cannot have the same id in more than one element.

Put in the input that you want to clear an attribute data-*, for example:

<input data-qtd="Qtd"...>

And in the complete ajax:

$("input[data-qtd='Qtd']").val('')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.