【问题标题】:fill the input type hidden from selected multiple option on jquery填充从jquery上选择的多个选项中隐藏的输入类型
【发布时间】:2015-05-16 09:44:54
【问题描述】:

我有一个这样的输入:

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

            }
        });
    });

非常感谢您的帮助...

编辑这是小提琴:JSFIDDLE

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    更新了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); 
      });
    

    评论版学习之用

       $("#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);
    
      });
    

    【讨论】:

    • 你知道,它并不真正成功,因为第一次选择时会给出结果:“email1”。第二个选择的是“email1,email1,email2,”
    • 这段代码没有这样做,但我会检查一下小提琴,看看问题出在哪里。
    • 谢谢,但是没有像 emai1、email2、email3 这样的空间。那里像email1,email2,email3。顺便说一句,我太傻了。
    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    相关资源
    最近更新 更多