简体   繁体   English

Jquery脚本 - 使用输入文本填充选择,反之亦然

[英]Jquery script - Fill select with input text and vice versa

I have 2 scripts that I need to combine, I basically have a script that allows a text input to be filled when you select an item in a select list. 我有2个脚本需要组合,我基本上有一个脚本,允许在选择列表中选择项目时填充文本输入。 I also have a script that fills a select list based on input of a text input. 我还有一个脚本,根据文本输入的输入填充选择列表。 Now I am trying to figure out how can I combine these 2 scripts so they can work together. 现在我想弄清楚我如何组合这两个脚本,以便它们可以一起工作。

Heres an example of a multi select filling a text input. 下面是多选填充文本输入的示例。 http://jsfiddle.net/akhyp/1963/ http://jsfiddle.net/akhyp/1963/

    function loopSelected()
{
  var txtSelectedValuesObj = document.getElementById('hidden_input');
  var selectedArray = new Array();
  var selObj = document.getElementById('selected_items');
  var i;
  var count = 0;
  for (i=0; i<selObj.options.length; i++) {
    if (selObj.options[i].selected) {
      selectedArray[count] = selObj.options[i].value;
      count++;
    }
  }
  txtSelectedValuesObj.value = selectedArray;
}

Heres an example of a text input filling a multi select based on input. 下面是基于输入填充多选的文本输入的示例。 http://jsfiddle.net/akhyp/1964/ http://jsfiddle.net/akhyp/1964/

  $("#txt").change(function(e){
    var ar = $(this).val().split(",");
    $("#sel option").each(function(){
        if(ar.indexOf($(this).val()) != -1)
           $(this).attr("selected","selected");
    });
  });

What I am trying to accomplish is something like this: If text input is null then fill text input with selected option from the select list. 我想要完成的是这样的:如果文本输入为null,则使用选择列表中的选定选项填充文本输入。 If the text input is already filled then fill in the multi select based on input from the text input. 如果文本输入已填满,则根据文本输入的输入填写多重选择。

Any help would be appreciated. 任何帮助,将不胜感激。

Final update: Finally got the results I wanted, here the final product http://jsfiddle.net/akhyp/1966/ been working on this for weeks possibly months lol. 最后的更新:终于得到了我想要的结果,这里最终产品http://jsfiddle.net/akhyp/1966/已经在这个工作了几个星期可能几个月大声笑。 Really happy to have this working. 真的很高兴有这个工作。

I added another change script function, seems to have made it work. 我添加了另一个更改脚本函数,似乎已经使它工作了。 Your change set the selected, and mine transfers the values. 您的更改设置了所选,我的传输值。

$('#sel').change(function(){
    $('#txt').val($('#sel').val());
});

http://jsfiddle.net/snlacks/akhyp/1965/ http://jsfiddle.net/snlacks/akhyp/1965/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM