如果选项包含子字符串,则使用字符串数组和嵌套循环从下拉列表中选择选项(Using a string array and a nested loop to select options from dro

编程入门 行业动态 更新时间:2024-10-27 12:27:50
如果选项包含子字符串,则使用字符串数组和嵌套循环从下拉列表中选择选项(Using a string array and a nested loop to select options from dropdown if option contains substring)

我有一定数量的选择(我在本例中使用了三个)。 每个选择都有相同的选项,分别是:“某个数字” - “名称”。

我有一个只有名称的数组,表明每个选项中有哪些选项。 数组元素和选择元素的数量始终相同。 顺序将始终相同,这意味着,应在第一个选择中选择数组的第一个名称(id以“_1”结尾的选择)。

因此在示例中,数组中有三个名称和三个选择元素。 应在select_1中选择标记,在select_2中选择Susan,在select_3中选择John。

由于数组中的名称不完全匹配(它们没有:“some number” - ),我尝试检查每个select中的选项是否包含使用嵌套lop的名称。 但是在结果中,所有选择都选择了第三个名称John。

为什么会这样? 提前致谢。

$(document).ready(function() {
  //Array with names
  var names = ["Mark", "Susan", "John"];
  var arrayLength = names.length;
  /* OLD CODE
  for(i = 0; i < 3; i++){
    for(j = 0; j < arrayLength; j++){
      $("#select_" + (i+1) + " option:contains(" + names[j] + ")").prop("selected", true);
    }
  }
  */
  
  //SOLUTION
  for(j = 0; j < arrayLength; j++){
        $("#select_" + (j+1) + " option:contains(" + names[j] + ")").prop("selected", true);
    }
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select name="select_1" id="select_1">
  <option value="0">Select</option>
  <option value="1">456 - Susan</option>
  <option value="2">335 - John</option>
  <option value="3">887 - Mark</option>
  <option value="4">182 - Amy</option>
</select>
<select name="select_2" id="select_2">
  <option value="0">Select</option>
  <option value="1">456 - Susan</option>
  <option value="2">335 - John</option>
  <option value="3">887 - Mark</option>
  <option value="4">182 - Amy</option>
</select>
<select name="select_3" id="select_3">
  <option value="0">Select</option>
  <option value="1">456 - Susan</option>
  <option value="2">335 - John</option>
  <option value="3">887 - Mark</option>
  <option value="4">182 - Amy</option>
</select> 
  
 

I have a certain number of selects (I used three in this example). Each select has the same options, which are : "some number" - "name".

I have an array with names only that indicates which options belong in each select. The array elements and the number of select elements are always the same. The order will always be the same, this means, the first name of the array should be selected in the first select (the select with id ending in "_1").

So in the example, three names in the array and three select elements. Mark should be selected in select_1, Susan in select_2 and John in select_3.

Since the names in the array don't match exactly (they don't have: "some number" - ), I try to check if the options in each select contains the name using a nested lop. However in the result, all the selects have the third name John as selected.

Why is this happening? Thanks in advance.

$(document).ready(function() {
  //Array with names
  var names = ["Mark", "Susan", "John"];
  var arrayLength = names.length;
  /* OLD CODE
  for(i = 0; i < 3; i++){
    for(j = 0; j < arrayLength; j++){
      $("#select_" + (i+1) + " option:contains(" + names[j] + ")").prop("selected", true);
    }
  }
  */
  
  //SOLUTION
  for(j = 0; j < arrayLength; j++){
        $("#select_" + (j+1) + " option:contains(" + names[j] + ")").prop("selected", true);
    }
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select name="select_1" id="select_1">
  <option value="0">Select</option>
  <option value="1">456 - Susan</option>
  <option value="2">335 - John</option>
  <option value="3">887 - Mark</option>
  <option value="4">182 - Amy</option>
</select>
<select name="select_2" id="select_2">
  <option value="0">Select</option>
  <option value="1">456 - Susan</option>
  <option value="2">335 - John</option>
  <option value="3">887 - Mark</option>
  <option value="4">182 - Amy</option>
</select>
<select name="select_3" id="select_3">
  <option value="0">Select</option>
  <option value="1">456 - Susan</option>
  <option value="2">335 - John</option>
  <option value="3">887 - Mark</option>
  <option value="4">182 - Amy</option>
</select> 
  
 

最满意答案

您可以在此处将i更改为j:

$("#select_" + (j+1) + " option:contains(" + names[j] + ")").prop("selected", true);

但是这里不需要嵌套循环。 一个就够了:

for(j = 0; j < arrayLength; j++){ $("#select_" + (j+1) + " option:contains(" + names[j] + ")").prop("selected", true); }

You can just change i to j here:

$("#select_" + (j+1) + " option:contains(" + names[j] + ")").prop("selected", true);

But you don't need nested loops here. One is enough:

for(j = 0; j < arrayLength; j++){ $("#select_" + (j+1) + " option:contains(" + names[j] + ")").prop("selected", true); }

更多推荐

本文发布于:2023-07-29 05:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1312614.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   选项   嵌套   数组   列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!