jQuery脚本-使用输入文本填充选择,反之亦然 [英] Jquery script - Fill select with input text and vice versa

查看:72
本文介绍了jQuery脚本-使用输入文本填充选择,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个脚本需要组合,基本上我有一个脚本,当您在选择列表中选择一个项目时,该文本允许填充文本输入.我也有一个脚本,可以根据文本输入的输入来填充选择列表.现在,我试图弄清楚如何结合这两个脚本,以便它们可以一起工作.

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. 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.

以下是填充文本输入的多选示例. http://jsfiddle.net/akhyp/1963/

Heres an example of a multi select filling a text input. 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;
}

这里有一个文本输入示例,该示例输入基于输入进行多重选择. http://jsfiddle.net/akhyp/1964/

Heres an example of a text input filling a multi select based on input. 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. If the text input is already filled then fill in the multi select based on input from the text input.

任何帮助将不胜感激.

最终更新: 终于得到了我想要的结果,这是最终产品 http://jsfiddle.net/akhyp/1966/已经为此工作了可能长达数月的大声笑.真的很高兴有这个工作.

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. 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/

这篇关于jQuery脚本-使用输入文本填充选择,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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