获取选择值列表作为隐藏字段中的管道分隔并提交

时间:2017-09-17 05:39:47

标签: javascript php jquery html forms

我有一个带有两个选择列表的网络表单,用户将他们想要的项目从一个选择框移动到另一个选择框,当用户点击“保存”按钮时我想要获取选择框中所有值的列表命名"两个"并将它们输入隐藏字段,称为" TwoArray"然后提交表格。我以为我可以使用.map来做到这一点,但我还没有能够弄明白,到目前为止,代码还在下面。注意:我想要选择名称为二的所有值的分隔列表,无论它们是否被选中"或不。请帮忙!此示例中隐藏字段的输出应为" 103,109"

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller='MyController' ng-app="myApp">
  <input type="text" ng-model="price">
  <select name="operator" id="test" ng-model="selected" ng-options="x as x.title for x in operators"></select>
  <table border="1">
    <tr>
      <th>Name</th>
      <th>Redemptions</th>
    </tr>
    <tr ng-repeat="j in data | priceGreaterThan: {price: price, operator: selected.field}">
      <td>{{j.name}}</td>
      <td>{{j.redemptions}}</td>
    </tr>
  </table>
</div>

1 个答案:

答案 0 :(得分:0)

尝试使用数组进行存储,然后使用$.each()循环捕获选项值的子项。我将此函数放在JavaScript的最底层,在所有其他侦听器下:

// Listen for an input click (you can listen for whatever)
$('input').on('click',function(e) {
    // Set an array
    var getAllVals  =   [];
    // Loop through this select's options (children)
    $.each($('#Two').children(),function(k,v) {
        // Store the "value" attribute values
        getAllVals.push($(v).attr('value'));
    });
    // Place the imploded array into the field
    $('input[name=TwoArray]').val(getAllVals.join(','));
});

这是一个jsFiddle演示,输入为text,因此您可以看到它发生变化:

https://jsfiddle.net/kc66x5sr/