【问题标题】:How do I populate one select from a choice in another select using jquery and a json array如何使用 jquery 和 json 数组从另一个选择中的选择中填充一个选择
【发布时间】:2016-06-26 04:25:09
【问题描述】:

这是我的选择和一个 json 数组,我可以使用它来根据选择填充第二个 div。

我想先删除第二个选择选项,然后填充选项并将所选值设置为第一项

<select name="select1" id="select1" class="form-control">
    <option value="2">Theme 1</option>
    <option value="1">Theme 2</option>
    <option value="0">unnamed theme</option>
</select>

<select name="select2" id="select2" class="form-control">
    <option value="49">Products</option>
</select>

json 数组(使用 PHP 从数据库值创建以将其回显)

{"2":{"49":"Products"},"1":{"48":"Product 48","50":"Opportunity","51":"Enroll"},"0":{"52":"Discover product 52","53":"Moringa Support","54":"Product 54","55":"product 55"}}

【问题讨论】:

  • 显然您发布了这个问题是为了发布您已经提出的解决方案,这很好,但是您的问题似乎过于本地化,以至于该解决方案对其他人没有多大用处。恕我直言,解决方案需要更抽象才能对其他人有用
  • 您的代码不必很复杂就可以执行复杂的任务。老实说,我的评论旨在帮助您理解这一点。您的代码是为解决您的特定问题而编写的,它反映了,例如,查看 JSON 的结构,该功能取决于此特定结构,但该结构仅对您有意义,并且将是在其他人身上迷失了一个更可重用的结构可能类似于{"options":[{"value":"someValue", "text":"someText"}]} 明白我的意思吗?这可能对其他有类似问题的人有用
  • 但我想当你使用loopCounter 来跟踪.each() 循环中已经可以访问相同的值时,我想我不能指望你理解这样的概念值通过index 变量,在全局范围内无缘无故地声明它们的所有变量,并编写两次解决简单问题所需的代码,然后将代码发布到网上,并带有“嘿,看看我制造“无意识的笑容;)
  • 我在下面添加了一个答案以向您展示我的意思,希望您能将其视为一个真诚地试图帮助您的程序员,而不是像您上面的评论表明您可能那样做出反应,无论哪种方式我希望它可以帮助你学习。快乐编码:)

标签: jquery json select selection populate


【解决方案1】:

以下是我将如何做到这一点。

基本上它是这样工作的:

  • 在您的第一次选择中添加课程select-master
  • 添加数据属性select-info,其值为保存所需JSON的变量的名称
  • 添加一个数据属性select-slave,其值为第二个选择框的选择器
  • 将事件处理程序附加到类select-master 的任何选择上
  • select-master 发生变化时,使用选定的值从 JSON 中获取选项,获取第二个选择,用选项填充它

注意,我更改了您的 JSON 的结构。我这样做是为了让结构有意义。

$('.select-master').change(function(){
     var $this = $(this);
     var val = $this.val();
     var possibleSelections =window[$this.data('select-info')]; // get the JSON for this box, you could store the JSON in the data attribute itself but this method makes it easier to use the same JSON for multiple boxes easily if needed
     var $slave = $($this.data('select-slave')).html(''); // get the selector for the other select box, find the box, and clear it's contents all in one go
     var selection = possibleSelections[val];
     if(selection){
         $.each(selection.options,function(i,option){
             var selected = i == 0 ? 'selected' : '';
             $slave.append('<option selected="'+selected+'" value="'+option.value+'">'+option.text+'</option>');
         });
     }
});

var selections = {
  "0": {
    options: [{
      value: 52,
      text: "Discover product 52"
    }, {
      value: 53,
      text: "Moringa Support"
    }, {
      value: 54,
      text: "Product 54"
    }, {
      value: 55,
      text: "product 55"
    }]
  },
  "1": {
    options: [{
      value: 48,
      text: "Product 48"
    }, {
      value: 50,
      text: "Opportunity"
    }, {
      value: 51,
      text: "Enroll"
    }]
  },
  "2": {
    options: [{
      value: 49,
      text: "Products"
    }]
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select name="select1" id="select1" class="form-control select-master" data-select-slave="#select2" data-select-info="selections">
  <option value="2">Theme 1</option>
  <option value="1">Theme 2</option>
  <option value="0">unnamed theme</option>
</select>

<select name="select2" id="select2" class="form-control">
  <option value="49">Products</option>
</select>

【讨论】:

  • @DamonHogan 从数据库中添加您想要的任何可能值就像在生成页面 html 时调用 var selections = &lt;?php echo $selections; ?&gt; 或您选择的任何其他方法一样简单。重点是selections 是一个变量,可以随意设置。我的所有观点仍然有效,您的方法仍然存在太多缺陷,无法在评论中指出。这是一个更好的解决方案,即使您出于恶意而否决了它。快乐编码 ;)
猜你喜欢
  • 2011-08-17
  • 2019-06-13
  • 1970-01-01
  • 2020-07-31
  • 2015-02-11
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 2014-09-17
相关资源
最近更新 更多