下拉列表中的更改会更新同一动态创建的行中的文本框
今天早上你非常乐于助人,我想我能在这方面获得帮助是一个不错的选择。我在表中动态创建了行,其中一个字段是一个用于选择费用类型的下拉列表。更改后,我想用 $$ 金额更新同一行中的字段。我从警报中获得了正确的费用和正确的 ID(“费用为 class_fee[37],金额为 225.00”),但包含费用 $$ 的文本框未更新。 文本框 ID 为 id="class_fee[]"
这是 jQuery(也许我在识别 $$ 框的方式上全错了?):
$(".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 || '');
}
});
这是 foreach 循环内的实际 HTML:(
<td>
<input type="text"
name="students[<?php echo $i ?>][class_fee]"
id="class_fee[<?php echo $i ?>]"
value="<?= html($class['class_fee']) ?>"
size="6" >
</td>
是的,有括号。$i 是来自类注册表。)
这是一个 json 响应:
{"fee_amount":"25.00"}
这些看起来都是很好的答案,我已经尝试了所有这些,但是费用金额的文本框没有改变。我在 Firebug 中没有任何错误。我很困惑。
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="students[<?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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为元素在文档加载后被注入到 DOM 树中,所以不能使用
.bind()
。您应该使用.live()
或比.delegate()
更好的http://api.jquery.com/live/
http://api.jquery .com/delegate/
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()
http://api.jquery.com/live/
http://api.jquery.com/delegate/
目前,
$class_fee
包含class_fee[37]
,而您想要的 id(可能)是class_fee37
。像这样的东西应该可以工作,我想
如果不行,请发布文本框的 HTML。
At the moment,
$class_fee
containsclass_fee[37]
, whereas the id you want (probably) isclass_fee37
.Something like this should work, I think
If it doesn't, please post the HTML for the textbox.