【问题标题】:Populating One Select Box Based on Another When Selects are ArraysPopulating One Select Box Based on Another When Selects are Arrays
【发布时间】:2013-04-02 03:10:54
【问题描述】:

I know how to populate one select box based on the option from another using jquery and ajax, but how do you do it when the select boxes are arrays?

默认情况下,页面上有 5 辆,但我也使用 .EnableMultiField() 根据需要添加更多行...在任何给定的一周,我们都会在库存中添加 10 到 30 辆汽车。

阿贾克斯:

$(".make").change(function() {
var id=$(this).val();
var dataString = 'id='+ id;

$.ajax ({
    type: "POST",
    url: "get_models.php",
    data: dataString,
    cache: false,
    success: function(html) {
        $(".model").html(html);
    } 
});

});

PHP:

            <ul>
            <li><label for="year[]">Year</label><select name="year[]"><option></option>'.$Year.'</select></li>
            <li><label for="year[]">Make</label><select name="make[]" class="make"><option></option>'.$Make.'</select></li>
            <li><label for="year[]">Model</label><select name="model[]" class="model" style="width: 120px"><option></option>'.$Model.'</select></li>
            <li><label for="trim[]">Trim</label><input type="text" size="10" name="trim[]" /></li>
            <li><label for="vin[]">VIN</label><input type="text" name="vin[]"size="20" maxlength="17" onkeyup="return Upper(event,this)" /></li>
            <li><label for="mileage[]">Mileage</label><input type="text" name="mileage[]" size="3" maxlength="6" /></li>
            <li><label for="color[]">Color</label><select name="color[]"><option></option>'.$Color.'</select></li>
            <li><label for="cost[]">Cost</label><input type="text" name="cost[]" size="6" maxlength="6" onchange="currency(this)" /></li>
            <li><label for="asking[]">Asking</label><input type="text" name="asking[]" size="6" maxlength="6" onchange="currency(this)" /></li>
        </ul>

【问题讨论】:

  • var dataString = \'id=\'+ id;\' 是来自真实代码还是只是问题中的一些错字?
  • 我复制并粘贴... jquery 代码被包装在 php 中,所以我转义了引号。此代码有效,但仅适用于第一个“make”字段
  • This code works, but only on the first "make" field。标记只有一个make[] 字段。其他人在哪里?
  • 我只发了一个sn-p,其余的都在,只是没发。见:login.carcityofdanbury.com/New/01/add.php

标签: php jquery


【解决方案1】:

我认为您的问题是您通过 AJAX 加载的模型全部设置为 .model。您需要将您的$('.make').change(...) 更改为关注

        $(".make").change(function () {
           var $this = $(this);
           var id = $(this).val();
           var dataString = 'id=' + id;

           $.ajax({
              type: "POST",
              url: "get_models.php",
              data: dataString,
              cache: false,
              success: function (html) {
                 $(".model", $this.closest('ul')).html(html);
              }
           });
        });

这将为 .model 设置新的“模型”,该模型存在于已更改的 .make&lt;ul&gt;

更新

为了适应动态添加的.make,替换

$(".make").change(function () {

$(document).on('change', 'ul .make', function () {

【讨论】:

  • 实际上,几乎做到了...它可以与页面上的原始 6 个品牌和模型一起正常工作,但是如果我添加更多(通过 EnableMultiField() 脚本)这些新字段可以不工作。
  • 你很好!这解决了所有行,无论我添加多少
【解决方案2】:

我对答案的建议是您的前三个&lt;label&gt; 错误地指向year[] 而不是year[]make[]model[]。这是扔掉你的东西吗?它击中第一个“制造”领域并抛出。对我来说很有意义...

祝你好运(:

【讨论】:

猜你喜欢
  • 2015-04-16
  • 2015-05-01
  • 1970-01-01
  • 2022-12-28
  • 2019-07-08
  • 2020-06-12
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
相关资源
最近更新 更多