How to recover values of fields loaded with jQuery

Asked

Viewed 3,697 times

6

Hello, I recently ran into a problem loading div content dynamically using jQuery, but it turns out that when I upload the content to that div, I can’t recover the values that are sent from it.

The content loaded in the div is a form, which when clicked on the "add" button, it loads another function in the "onclick" event of that same button, but when I click on that button the div returns to the value set in the default of the switch and does not load the event function placed on the button.

One of the forms loaded in this div is the addition of products:

<form method="POST">
<input type="text" name="produto"/>
<input type="number" name="preco"/>
<textarea name="descricao"></textarea>
<select name="categoria">
 <option value="1">1</option>
 <option value="1">1</option>
</select>
<button onclick="add()">Adicionar</button>

For example, when pressing the "Add" button, if the add() function was executed without error, a message was displayed in the div "staus" and remained on the same page instead of returning to the default page

Note: the add() function collects the data coming from the form. Part of the script that deals with this form:

produto = $('input[name=xxx]').val();
$.post('adicionar.php',{produto:produto},function(i){$('#staus').html(i)});

http://jsfiddle.net/Xb576/1/

  • can give an example of html that is loaded into the div?

  • The example is available since I published the question at the end of the question.

  • Edilson, what I wanted to see is the content that you will dynamically search for. The Form and the rest of the content that is loaded.

2 answers

3


EDIT: the div #staus is within the element #direita which is the target of the method load, and will therefore be replaced by form that is coming from the server. Therefore, when clicking the button, the div #status it will no longer exist because it has been deleted from the page.

load is asynchronous

The method load jquery is asynchronous. To access the elements brought by the method load jquery, you should add a callback function for when the request is ready:

$('#direita').load("url", 
     function (responseText, textStatus, XMLHttpRequest) {
         if (textStatus == "success") {
             // o request funcionou e os elementos já devem estar disponíveis
             // aqui você pode associar eventos aos elementos trazidos via AJAX
         }
         if (textStatus == "error") {
             // o request não funcionou, você terá de informar o usuário de que
             // algo deu errado
         }
     }

Other remarks in your code

The case of switch is done by placing only the value to be compared, and not by making an assignment in the case.

switch(mostrar){
    case 'add': // errado: case mostrar = 'add' 
        break;
    case 'editar': // errado: case mostrar = 'editar' 
        break;
    default:
}
  • Ignoring the switch, and passing the part where I can’t get the value of the selectors loaded in that div -, because the switch still works, although I have passed an argument the condition remains correct

  • Okay, I’m on linux at the moment, but I’ll be there as soon as possible, so thank you

  • It didn’t work, it does the same thing my script did :

1

This is because when delegating the event to the button, it does not yet exist in the DOM. For the event to work with an element that will still be loaded into the DOM, you can delegate the event to an existing parent element and add a filter to the actual desired selector.

Example:

$(document).on('click', 'button', function(event) {

    // 

});

Whereas $(document) is the element to which the event is delegated, and 'button' the filter that corresponds to a possible element that already exists or will appear within $(document).

  • Just to clarify, my problem is not simply the event, because I can’t even get the values within that same div, and my goal is to get the form values in the div. Type print an Alert content the value of the input in that div for example.

Browser other questions tagged

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