javascript - 填充从 jquery 上的选定多个选项中隐藏的输入类型

标签 javascript jquery html

我有一个类型如下的输入:

<input class="emailSend" name="emailSend" type="hidden">

然后我有一个像这样的多选选项

<div class="controls">
      <select id="email" multiple data-rel="chosen" class="input-xlarge" name="email[]">
          <?php
              foreach ($atasan as $data) {
                  echo "<option value='" . $data['email'] . "'>" . $data['email'] . "</option>";
              }
        ?>

       </select>
   </div>

我的问题是,我想从从多个选择选项中选择的选项中填充隐藏的输入。比方说,选择的选项是“email1”、“email2”、“email3”,然后会影响隐藏类型,例如“email1、email2、email3”。

我已经在 jquery 中尝试了 3 个小时,但我被卡住了。我的代码是这样的。

$("#email").change(function() {

        var elements = $("#email option:selected").length;
        var input = $(".emailSend");

        $(".emailSend").html("");
        $.each($("#email option:selected"), function(/*index, element*/) {

            input.val(input.val() + $(this).html() + ", ");
            if (index < elements - 1) {
               //code to remove last comma
               //any idea ?

            }
        });
    });

非常感谢您的帮助...

编辑 这是 fiddle :JSFIDDLE

最佳答案

已更新 FIDDLE现在我明白你说的是看你制作的 fiddle 是什么意思了。

这实际上就是您需要做的...

已更新以在地址之间包含空格!

  $("#email").on('change', function() {
       var thisval = $(this).val() + '';
       var myarr = thisval.split(',');
       var newval = '';
       myarr.forEach(function(i,v) {
           newval += i + ' , ';            
       });
       newval = newval.slice(0, newval.length - 3);
       $("#emailsend").val(newval); 
  });

Commented Version (for learning and stuff)

   $("#email").on('change', function() {

       //the extra space at the end is to typecast to string
       var thisval = $(this).val() + '';

       //takes string of comma separated values and puts them
       //into an array
       var myarr = thisval.split(',');

       //Initialize a new string variable and loop through 
       //the array we just created with MDN's forEach()
       var newval = '';
       myarr.forEach(function(i,v) {

           //add to the string through each iteration, 
           //including comma with spaces
           newval += i + ' , ';

       });

       //use .slice() to trim three characters off the
       //end of the final string. (strip final comma)
       newval = newval.slice(0, newval.length - 3);

       //and last but not least, assign our newly created 
       //and properly formatted string to our input element. 
       $("#emailsend").val(newval);

  });

关于javascript - 填充从 jquery 上的选定多个选项中隐藏的输入类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30270245/

相关文章:

javascript - 要求是只删除浏览器窗口上的地址栏,但保留其余的工具栏选项

javascript - jQuery 无法处理 AJAX 加载的内容来显示/隐藏 div 内容

Javascript:用字符串开头的 x 空格替换 x &nbsp

查询。如果窗口大小 <= 某个值,则启动 jquery。但是在某些情况下,如果屏幕 > 特定值,则 jquery 会执行

asp.net - 调整菜单以覆盖菜单宽度

html - 使用 CSS 将我页面上的所有内容居中 - 菜单栏、表格等

javascript - 如何确定一个点当前位于椭圆路径的哪一半?

javascript - Chart.js 图表通过 MVC Controller 传递数据

jquery - 重置jquery minicolors颜色值

html - @media 查询不适用于移动设备