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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助