使用jQuery设置select2输入框的值

4
我将尝试使用jquery设置select2的值。这可能是一个重复的问题,但我在这里已经阅读了30个不同的回答,没有找到解决我的问题的方法。
HTML代码:
<div class="has_many_fields" data-required-has-many-fields-rows="1" id="location_account_associations"><input id="location_account_associations_attributes_0__destroy" name="location[account_associations_attributes][0][_destroy]" type="hidden" value="false" />
<div class="has_many_fields_row countable" id="location_account_associations_attributes_0">
    <div class="form__row form__row--spreadsheet">
        <div class="form__row__body">
            <div class="form__row__field-wrapper">
                <ul class="grid grid--no-gutters">
                    <li class="grid__6 grid__x--show">
                        Account Thing
                        <input id="location_account_associations_attributes_0_ab_account_id" name="location[account_associations_attributes][0][ab_account_id]" type="hidden" value="42" />
                    </li>
                    <li class="grid__5">
                        <select class="js-select2-combobox" id="location_account_associations_attributes_0_account_id" name="location[account_associations_attributes][0][account_id]" placeholder=" " reflection="#&lt;ActiveRecord::Reflection::AssociationReflection:0x012fb5asd200e8&gt;">
                            <option value=""></option>
                            <option value="1">Orange</option>
                            <option value="2">Apple</option>
                        </select>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

我尝试了多种不同的方法来选择“Orange”。请注意,在按下某个按键时执行此jQuery。另外,由于多种原因,HTML不能编辑。只有jQuery可以更改。

Javascript / jQuery:

(function() {
    $(document).on("keydown", function(e) {

        // ignore unless CTRL + ALT/COMMAND + N was pressed
        if (!(e.keyCode == 78 && e.ctrlKey && e.altKey )) return

        jQuery('[name*="[account_associations_attributes][0]').select2("val",1);
        jQuery('[name*="[account_associations_attributes][0]').select2("val","Orange");
     })
 }())
3个回答

6

最终找到了解决方案。

jQuery('[name*="[account_associations_attributes][0]"]').select2('val', '1')

谢谢。这个有用。有没有办法实现多选/多值?例如,值1、2和5。 - ewroman
我从这里找到了 select2('val', ['1','2','5']),链接为 https://dev59.com/Dmcs5IYBdhLWcg3wYzH6。 - ewroman

2

试试这个:
你可以通过选择id来选择它。
在线演示:http://jsfiddle.net/5Y9mG/10/

  $(document).ready(function(){

    $(document).on("keydown", function(e) {

        // ignore unless CTRL + ALT/COMMAND + N was pressed
     if (!(e.keyCode == 78 && e.ctrlKey && e.altKey )) return;
        $('#location_account_associations_attributes_0_account_id option:eq(1)').prop('selected', true)
     });
});

您还可以通过id和value来选择它,像这样:

 $('#location_account_associations_attributes_0_account_id option[value="1"]').prop('selected', true)

@Zack:你试过这个吗? - Awlad Liton
我之前没有尝试过,但现在我尝试了一下,它不起作用。我编辑了问题,包括更多的JavaScript代码...也许这会有所帮助? - Zack
@Zack:按下CTRL + ALT + N,然后它将在您的选择框中选择橙色。 - Awlad Liton
抱歉,我出去吃午餐了。还是不行。我想不出为什么。我会试着做一些实验。 - Zack
你在实时演示中测试过吗?按下CTRL + ALT + N。 - Awlad Liton
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/44983/discussion-between-awlad-liton-and-zack - Awlad Liton

-1
检测多个按键,特别是带有修饰键的按键,非常棘手。
请查看此其他帖子,然后确保您的按键事件生成更基本的结果(例如简单的alert()),然后再添加更复杂的行为,例如更改下拉列表的值,这也取决于浏览器和下拉列表本身的情况。

是的...我试过了...事件没问题。我有大约50个其他的东西在事件上运行。这是唯一一个不起作用的。 - Zack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接