开发者

How to get value of textbox after ajax post using JQuery

开发者 https://www.devze.com 2023-04-05 03:28 出处:网络
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?

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) {
        a开发者_StackOverflow社区lert($('#quantity['+key+']').val());
    });
 }

});


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());
});


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?


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()
0

精彩评论

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