使用同时在两者上选择的jquery填充另一个输入时,自动填充一个输入 [英] Autofill an input when another input is filled using jquery chosen on both

查看:110
本文介绍了使用同时在两者上选择的jquery填充另一个输入时,自动填充一个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我遇到问题时,我正在这里帮助另一个用户进行stackoverflow-我解释了如何解决问题,但是我无法使我的代码(我想作为示例添加)真正起作用.我对jQuery很陌生,因此,即使我了解基本知识以及可以做和不能做的事情,我仍然发现自己在努力完成简单的任务.

I was helping another user here on stackoverflow when I faced a problem - I explained how to solve something, but I couldn't get my code (that I wanted to add as an example) to actually work. I'm pretty new to jQuery, so even though I understand the basics and what can and can't be done, I still find myself struggling with simple tasks.

在这种情况下,我试图创建两个输入字段,都使用标记系统(选择了jQuery ).两个输入具有相同的选项列表,我希望当您为第一个输入输入一个项目时,它也会自动将其添加到第二个输入中.想法是您可以从第二个列表中删除任何所需的项目,而不会影响第一个.

In this case, I am trying to create two input fields, both using a tagging system (jQuery chosen). Both inputs have the same list of options, and I want that when you enter an item for the first input it automatically adds it to the second input as well. The idea is you can remove any item you want from the second list without affecting the first one.

您可以在此jsfiddle 中看到我的尝试.我尝试从第一个输入中提取值并对其进行循环,但是由于某种原因,它并没有真正超出第一项.

You can see my attempts in this jsfiddle. I tried extracting the value from the first input and looping over it, but for some reason it doesn't really loop beyond the first item.

我还尝试将第一项应用于第二个列表,如下所示:

I also tried to apply the first item to the second list like so:

$('#shipping option[text='+value.value+']').attr('selected', 'selected');

,然后在循环之后执行此操作:

and after the loop doing this:

$(".chosen-select").trigger('chosen:updated');

我知道这超出了第一个项目的范围,但实际上并没有奏效.我对下一步该做什么感到很困惑.显然,所选内容会更改输入,并且它的行为不会像通常的那样,但是我不知道如何解决此问题.任何帮助将不胜感激!

I knew this wouldn't work beyond the first item, but it didn't even work for that. I'm pretty confused at what I should do next. It's obvious that the chosen changes the input and it doens't behave like a usual one, but I can't understand how to work this out. Any help would be much appreciated!

推荐答案

使用此行:

$('#shipping option[value="'+value+'"]').prop('selected', true);

请注意,如果您选择美国"或英国",它们将在Chosen插件中显示两次,因为这些选项在html中存在两次,但是您可以使用:first选择器来避免这种情况

Beware that if you select "United States" or "United Kingdom", they will show twice in Chosen plugin, since those options exist twice in your html, but you can use the :first selector to avoid this

$('#shipping option[value="'+value+'"]:first').prop('selected', true);

编辑

以下代码在工作: http://jsfiddle.net/rf90210/uZBXM/2/

这篇关于使用同时在两者上选择的jquery填充另一个输入时,自动填充一个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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