jQuery以CSV格式获取剩余的OPTION值 [英] jQuery get remaining OPTION values in CSV format

查看:61
本文介绍了jQuery以CSV格式获取剩余的OPTION值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望返回CSV格式的字符串,以使用SELECT框中的OPTION值设置输入文本字段的值.

I'm looking to return a CSV formatted string to set the value of a input text field with the OPTION values from a SELECT box.

$.map($('#addedchargeamtid选项'), 函数(e){var exp = $(e).text();警报(exp); })

$.map($('#addedchargeamtid option'), function(e) { var exp = $(e).text(); alert(exp); })

在使用映射函数时,它看起来每次都会覆盖该值.无法弄清楚如何以某种方式将它们结合在一起.

In using a map function but it looks to overwrite the value each time. can't figure out how to concat these together somehow.

顺便说一句,感谢今天所有的jQuery Gurus,在我的问题上有很多道具!!!

BTW thanks to all the jQuery Gurus today, much props on my issues!!!

更新:

<option value="123">123</option>
<option value="asd">asd</option>
<option value="trr">trr</option>
<option value="345">345</option>

我需要这个:

123,asd,trr,345

但是这些选项是动态的,可以添加或删除,因此可以是1或100

But the options are dynamic and can be added or removed, so there could be 1 or 100

新:

这有点奏效.当我只添加一次时,它为我提供了同一项目的4个选项.也不会使用CSV值更新隐藏的文本字段

Well this is kinda working. it gives me 4 options of the same item when I only added it once. also does not update the hidden text field with the CSV value

// Add remove button
$('#addButton').click(function() {
   $('#removeButton').show();

   // Add 
   var myOptions = $('#typedchargeamtid').val();
   $.each(myOptions, function() {
      $('#addedchargeamtid').append(
         $('<option></option>').val(myOptions).html(myOptions)
      );
   });

   var txt = $('#addedchargeamtid').val() || [];
      $('#commasepchargeamtid').val(txt.join(','));
   });

再次感谢

推荐答案

您可以使用每一个代替地图:

You can use each instead of map:

    var options = Array();
    $('#addedchargeamtid option').each(function(index){
        options[index] = $(this).val();
    });

    $('#commasepchargeamtid').val(options.join(','));

这篇关于jQuery以CSV格式获取剩余的OPTION值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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