简体   繁体   English

在下拉字段中选择一个值,然后在多选字段中预先选择值

[英]Select a value on a drop down field with then pre selects values in a multi-select field

I have two fields. 我有两个领域。 I am dynamically pulling in values for each field. 我正在动态获取每个字段的值。 I can not hard code any of the data because it its changing. 我无法对任何数据进行硬编码,因为它在变化。

With javascript i want to select a value with the 1st single select drop down field. 使用javascript,我想使用第一个单选下拉列表选择一个值。 on click i want the second one to update with the matching ids. 点击我想让第二个更新匹配的ID。

ex (again values are dynamically getting pulled) ex(再次动态获取值)

field 1- single select drop down list Name (text displayed) - Id (value submitted) Fruits - 1 Vegetables - 2 Sweets - 3 Meat- 4 字段1-单选下拉列表名称(显示文本)-ID(提交值)水果-1蔬菜-2糖果-3肉类-4

Field 2- multi sect list box Name (text displayed) - Id (value submitted) Apple - 1 Carrot-2 Ice Cream - 3 Hamburger- 4 Orange- 1 Steak -4 Chocolate- 3 Raspberries -1 字段2-多派别列表框名称(显示的文本)-ID(提交的值)苹果-1胡萝卜2冰淇淋-3汉堡包-4橙子-1牛排-4巧克力-3树莓-1

Desired outcome: If the user selects Fruits from field 1 then the field 2 should pre select apple, orange, Raspberries (because they all have the same id of 1) 期望的结果:如果用户从字段1中选择“水果”,则字段2应预先选择苹果,橙子,覆盆子(因为它们的ID均为1)

I am dynamically pulling the data from salesforce in c# Page_Init data binding it to the fields 我正在从c#Page_Init数据中的Salesforce中动态提取数据,并将其绑定到字段

<asp:Panel CssClass="form-group" runat="server">
<label id="Field1" class="control-label" for="Field2" runat="server">
<asp:DropDownList ID="Field1" ClientIDMode="Static" 
onchange="myFunction(event)" CssClass="form-control" runat="server" />

<asp:Panel CssClass="form-group" runat="server">
<label id="field2" class="control-label" for="field2" runat="server"> 
</label>
<asp:ListBox ID="field2" CssClass="form-control" SelectionMode="Multiple" 
ClientIDMode="Static" runat="server" />
</asp:Panel>

<script type="text/javascript">
function myFunction(e) {
        document.getElementById("field2").value = e.target.value
    }
 </script>

I originally had this jquery in when i was just updating a single select drop down field. 我最初只是在更新单个选择下拉字段时才使用此jquery。 it is still selecting 1 value not all the values with the matching ids 它仍然选择1个值,而不是所有具有匹配ID的值

I ended up looking up a similar request and edited it to be dynamic values not hard coded 我最终查找了类似的请求,并将其编辑为动态值,而不是硬编码

<select name='field1' id="field1">
<option value="1">Fruits</option>
<option value="2">Vegetables</option>
<option value="3">Sweets</option>
<option value="4">Meat</option>
</select>

<select name='field2' id="field2" multiple >
<option value="1">Apple</option>
<option value="2">Carrot</option>
<option value="3">Ice Cream </option>
<option value="4">Hamburger</option>
<option value="1">Orange</option>
 <option value="4">Steak </option>
  <option value="3">Chocolate</option>
   <option value="1">Raspberries </option>
 </select>

 var foodsToSelect = $("#field1").val();
 var foods = document.getElementById( 'field2' );

 for ( var i = 0, l = foods.options.length, o; i < l; i++ )
 {
 o = foods.options[i];
 if ( foodsToSelect.indexOf( o.value ) != -1 )
 {
  o.selected = true;
}
}

http://jsfiddle.net/jw8rugse/2/ http://jsfiddle.net/jw8rugse/2/

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

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