0

I am getting undefined as a value using the code below when I try to get the value of a textbox after a post. Is this possible or am I missing something basic?

<input type="text" size="3" value="<?php echo $item['qty'] ?>" id="quantity[<?php echo $sizeid?>]" name="quantity[<?php echo $sizeid?>]">

$.ajax({
type: "POST",
url: "bin/process_updateqty.php",
dataType: 'json',
data: dataString,
success: function(data) {
    $.each(data, function(key, value) {
        alert($('#quantity['+key+']').val());
    });
 }

});

1
  • Are you saying that your return data is undefined? Have you parsed your json repsonse? data = $.parseJSON(data) ?? Commented Sep 14, 2011 at 22:28

3 Answers 3

1

Your success function will use the key from the each iterator- [0,1,2...], so your query selector is:

#quantity[0], #quantity[1], (etc)

Is this what you intended?

I think not. I think you probably wanted:

$.each(data, function(key, value) {
    alert($(value).val());
});
Sign up to request clarification or add additional context in comments.

Comments

0

We need some of your HTML, or at least the output of this (which is used for your id):

<?php echo $sizeid?>

In fact, as RSG says, the $.each method is used to iterate over an array, so in your function, the key parameter is the index of the element that is passed with the value parameter, as you can read in the documentation.

Also, are you sure that your data object is actually an array?

Comments

0

Thanks for all your answers. I figured it out.

The answer to the original question is - YES, you can reference a textbox after a postback. The issue was I was referencing a textbox array incorrectly.

I was originally referencing the textbox like this:

$('#quantity['+key+']').val()

However I should have been referencing the textbox this way:

$('input[name="quantity['+key+']"]').val()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.