下拉列表中的更改会更新同一动态创建的行中的文本框

发布于 2024-12-04 13:44:26 字数 1367 浏览 1 评论 0原文

今天早上你非常乐于助人,我想我能在这方面获得帮助是一个不错的选择。我在表中动态创建了行,其中一个字段是一个用于选择费用类型的下拉列表。更改后,我想用 $$ 金额更新同一行中的字段。我从警报中获得了正确的费用和正确的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久伴你 2024-12-11 13:44:26

因为元素在文档加载后被注入到 DOM 树中,所以不能使用 .bind()。您应该使用 .live() 或比 .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/

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/

何处潇湘 2024-12-11 13:44:26

目前,$class_fee 包含 class_fee[37],而您想要的 id(可能)是 class_fee37

像这样的东西应该可以工作,我想

jQuery('#class_fee' + this.id).val(fee.fee_amount || '');

如果不行,请发布文本框的 HTML。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文