Change in drop-down updates textbox in same dynamically created row
You were so very helpful this morning, that I thought it would be a good bet I could get help on this. I have dynamically created rows in a table, one field is a drop-down to select a fee type. When this is changed I want to update a field in the same row with the $$ amount. I'm getting the correct fee and the correct id from my alerts ("the fee is class_fee[37] and the amount is 225.00"), but the textbox with the fee $$ is not updating.
The the textbox id is id="class_fee[<?php echo $i ?>]"
Here is the jQuery (maybe I'm all wrong in how I'm identifying that $$ box?):
$(".fee_id_select").bind("change",function ()
{
var $class_fee = 'class_fee[' + this.id + ']';
jQuery.getJSON(
'get_fee_json.php',
{'id': this.value},
update_fee_amount
);
function update_fee_amount(data, textStatus)
{
var fee = data || {};
alert('the fee is ' + $class_fee + ' and the amount is ' + fee.fee_amount);
jQuery('#' + $class_fee).val(fee.fee_amount || '');
}
});
Here is the actual HTML inside a foreach loop:
<td>
<input type="text"
name="stude开发者_如何学Gonts[<?php echo $i ?>][class_fee]"
id="class_fee[<?php echo $i ?>]"
value="<?= html($class['class_fee']) ?>"
size="6" >
</td>
(yes, there are brackets. $i is the unique id number from a class registration table.)
Here is a jSon response:
{"fee_amount":"25.00"}
These all look like great answers, and I've tried them all, but the textbox for the fee amount is not changing. I don't have any errors in Firebug. I'm puzzled.
Because the element is injected into the DOM tree after document load you can't use .bind()
. You should use .live()
or better than that .delegate()
$(".fee_id_select").live("change",function ()
{
var $class_fee = 'class_fee[' + this.id + ']';
jQuery.getJSON(
'get_fee_json.php',
{'id': this.value},
update_fee_amount
);
function update_fee_amount(data, textStatus)
{
var fee = data || {};
alert('the fee is ' + $class_fee + ' and the amount is ' + fee.fee_amount);
jQuery('#' + $class_fee).val(fee.fee_amount || '');
}
});
http://api.jquery.com/live/
http://api.jquery.com/delegate/
At the moment, $class_fee
contains class_fee[37]
, whereas the id you want (probably) is class_fee37
.
Something like this should work, I think
jQuery('#class_fee' + this.id).val(fee.fee_amount || '');
If it doesn't, please post the HTML for the textbox.
精彩评论