dongnaopa6200 2015-02-16 21:51
浏览 20
已采纳

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

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.

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;
}

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.

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. Really happy to have this working.

  • 写回答

1条回答 默认 最新

  • dongyou26216708 2015-02-16 22:00
    关注

    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/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教