简体   繁体   English

连接选择框选定的值

[英]Concatenate select box selected values

Thanks in advance!提前致谢! I am building an application using asp.net web form that have one dropdownlist and two listbox, which needs to be clone whenever a button is click.我正在使用asp.net web 表单构建一个应用程序,它有一个下拉列表和两个列表框,只要单击按钮就需要克隆。 My problem is I'm currently hard coding a lot of code and I will like to make it more dynamic.我的问题是我目前正在硬编码很多代码,我想让它更加动态。 Can everyone help me figure this out?大家能帮我解决这个问题吗?

Here is an example of what my current script is doing.这是我当前脚本正在执行的操作的示例。 在此处输入图片说明

Code:代码:

$(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>

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM