连接选择框选择的值

时间:2018-08-16 21:23:00

标签: javascript jquery each

提前谢谢!我正在使用具有一个下拉列表和两个列表框的asp.net Web表单构建应用程序,每当单击按钮时都需要将其克隆。我的问题是我目前正在对很多代码进行硬编码,并且我想使其更具动态性。每个人都可以帮我解决这个问题吗?

这是我当前脚本正在执行的示例。 enter image description here

代码:

$(document).ready(function() {

  var clone = (function() {
    var cloneIndex = 0;
    var template = $('#categoryTemplate').text();

    return function() {
      return template.replace(/ID/g, ++cloneIndex);
    }
  })();

  //Start off with 1 category.
  categories.append(clone());

  var sbClone1 = new StringBuilder();

  $(document).on('change', '#MainContent_ddlWarehouse1', function () {
      var myele = '';
      sbClone1.strings[0] = [];
      var mystring = '';
      myele = $(this).val();
      sbClone1.strings[0] = 'SELECT top 10 ' + myele + ', PART_CODE, WAREHOUSE, UNIT_OF_MEASURE, MOVEMENT_DATE, MOVEMENT_CODE, [IC_MOVE_QUANTITY] FROM [dbo].[IC_MOVEMENTS] WHERE MOVEMENT_DATE between "' + startDateTextBox.val() + '" and "' + endDateTextBox.val() + '" AND WAREHOUSE = "' + myele + '" ';
      mystring = sbClone1.toString();

     $('#MainContent_hfConcatString').val(mystring);
     document.getElementById("MainContent_Label1").innerHTML = mystring;
   });

   $(document).on('change', '#MainContent_lbMovementCode1', function () {
       var myele = '';
       sbClone1.strings[1] = [];
       var mystring = '';
       myele = $(this).val();
       sbClone1.strings[1] = 'AND MOVEMENT_CODE IN "' + myele + '" ';
       mystring = sbClone1.toString();

       $('#MainContent_hfConcatString').val(mystring);
       document.getElementById("MainContent_Label1").innerHTML = mystring;
   });

   $(document).on('change', '#MainContent_lbUnitofMeasure1', function () {
      var myele = '';
      sbClone1.strings[2] = [];
      var mystring = '';
      myele = $(this).val();
      sbClone1.strings[2] = ' AND UNIT_OF_MEASURE IN "' + myele + '" ';
      mystring = sbClone1.toString();

      $('#MainContent_hfConcatString').val(mystring);
      document.getElementById("MainContent_Label1").innerHTML = mystring;
  });

  $(document).on("click", 'button.add', function() {
    categories.append(clone());
  });
});

<script type="text/template" id="categoryTemplate">
    <div class="category" id="DatasetID"> 
        <div class="row">
            <!-- Warehouse Category -->
            <div class="col col-lg-4">
                <div class="form-group">
                    <label class="control-label" for="WarehouseID">Warehouse</label>
                    <asp:DropDownList ID="ddlWarehouseID" runat="server" AppendDataBoundItems="true">
                        <asp:ListItem Value="0" Text="Please Select"></asp:ListItem>
                    </asp:DropDownList>
                </div>
            </div>
             <!-- MovementCode Category -->
            <div class="col col-lg-4">
                <div class="form-group">
                    <label class="control-label" for="MovementCodeID">Movement Code</label>
                    <asp:ListBox ID="lbMovementCodeID" runat="server" SelectionMode="Multiple">
                    </asp:ListBox>
                </div>
            </div>
             <!-- UnitofMeasure Category -->
            <div class="col col-lg-4">
                <div class="form-group">
                    <label class="control-label" for="UnitofMeasureID">Unit of Measure</label>
                    <asp:ListBox ID="lbUnitofMeasureID" runat="server" SelectionMode="Multiple">
                    </asp:ListBox>
                </div>
            </div>          
        </div>
    </div>
</script>

1 个答案:

答案 0 :(得分:-1)

无需遍历所有选项,请尝试以下操作:

var string = $("[name=cars]").val().join(", ")