如何基于另一个的值更新选择框? [英] How to update a select box based on the values of another?

查看:102
本文介绍了如何基于另一个的值更新选择框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails应用程序中,我有一个带有两个选择框的表单(其中一个是多个选择框):

In my Rails app I have a form with two select boxes (one of them being a multiple select box):

<label>Project owners</label>
<select multiple="multiple">
  <option value="1">Huey</option>
  <option value="2">Dewey</option>
  <option value="3">Louie</option>
</select>

<label>Invoice recipients</label>
<select>
  <option value="1">Huey</option>
  <option value="2">Dewey</option>
  <option value="3">Louie</option>
</select>

如何将invoice recipients限制为已选择的project owners?

How can I limit the invoice recipients to the project owners that have been selected?

例如,如果选择 Dewey Louie 作为project owners,则invoice recipients选择框应仅包含 Dewey Louie .

For example, if Dewey and Louie get selected as project owners, the invoice recipients select box should only contain Dewey and Louie as well.

我知道这可以通过jQuery实现,但是我的jQuery技能非常有限.

I know this is possible through jQuery but my jQuery skills are very limited.

请帮助我.非常感谢!

推荐答案

尝试

<label>Project owners</label>
<select id="owners" multiple="multiple">
  <option value="1">Huey</option>
  <option value="2">Dewey</option>
  <option value="3">Louie</option>
</select>

<label>Invoice recipients</label>
<select id="recipients">
  <option value="1">Huey</option>
  <option value="2">Dewey</option>
  <option value="3">Louie</option>
</select>

然后

$('#owners').change(function(){
    $('#recipients').empty().append($(this).find('option:selected').clone())
})

演示:小提琴

这篇关于如何基于另一个的值更新选择框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆