检索 ID 和数据,然后构建下拉菜单。

huangapple go评论106阅读模式
英文:

retrieve id and data and then build dropdown

问题

  1. 我从服务中收到以下数据。以下控制台日志在浏览器中打印如下:
  2. console.log("Inside data");
  3. console.log(data);
  4. Inside data
  5. [[id=1, name=BOTTLE-1], [id=2, name=BOTTLE-2], [id=4, name=BOTTLE-3]]
  6. 如果我想使用jQuery构建一个选项标签,是否容易提取`id`作为`1,3和4`以及`name`作为选项标签的值参数并构建一个选择下拉菜单?
  7. 例如,如果我有以下代码,并且想让它像现在显示的那样工作。
  8. <!-- begin snippet: js hide: false console: true babel: false -->
  9. <!-- language: lang-js -->
  10. var data = "[[id=1, name=BOTTLE-1], [id=2, name=BOTTLE-2], [id=4, name=BOTTLE-3]]";
  11. <!-- language: lang-html -->
  12. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  13. <select name="cars" id="cars">
  14. <option value="-">"Please Select"</option>
  15. <option id="1" value="BOTTLE-1">BOTTLE-1</option>
  16. <option id="3" value="BOTTLE-2">BOTTLE-2</option>
  17. <option id="4" value="BOTTLE-3">BOTTLE-3</option>
  18. </select>
  19. <!-- end snippet -->
  20. 使用`append`方法构建部分应该很容易,但我想知道提取所需值是否容易。
英文:

I have the following data I'm receiving from the service. The following console logs prints the following in browser:

  1. console.log(&quot;Inside data&quot;);
  2. console.log(data);
  3. Inside data
  4. [[id=1, name=BOTTLE-1], [id=2, name=BOTTLE-2], [id=4, name=BOTTLE-3]]

If I want to build a options tag using jQuery, is it easy to extract the id as 1,3 and 4 and name for the value parameter of options tag and build a select dropdown?

For example, if I've the following code and if I want to work it like it is showing now.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. var data = &quot;[[id=1, name=BOTTLE-1], [id=2, name=BOTTLE-2], [id=4, name=BOTTLE-3]]&quot;;

<!-- language: lang-html -->

  1. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;select name=&quot;cars&quot; id=&quot;cars&quot;&gt;
  3. &lt;option value=&quot;-&quot;&gt;&quot;Please Select&quot;&lt;/option&gt;
  4. &lt;option id = &quot;1&quot; value=&quot;BOTTLE-1&quot;&gt;BOTTLE-1&lt;/option&gt;
  5. &lt;option id = &quot;3&quot; value=&quot;BOTTLE-2&quot;&gt;BOTTLE-2&lt;/option&gt;
  6. &lt;option id=&quot;4&quot; value=&quot;BOTTLE-3&quot;&gt;BOTTLE-3&lt;/option&gt;
  7. &lt;/select&gt;

<!-- end snippet -->

Building part using append method should be easy but I am wondering if it's easy to extract the required values

答案1

得分: 0

以下是您的解决方案的翻译部分:

  1. let data = "[[id=1, name=BOTTLE-1], [id=2, name=BOTTLE-2], [id=4, name=BOTTLE-3]]";
  2. let codingRegex = /id=\d+, name=BOTTLE-\d+/g;
  3. let matches = data.match(codingRegex);
  4. let cars = $("#cars");
  5. matches.forEach(match => {
  6. let temp = match.split(", ");
  7. let id = temp[0].replace("id=", ""),
  8. name = temp[1].replace("name=", "");
  9. let option = new Option();
  10. option.text = name;
  11. option.value = id;
  12. cars.append(option);
  13. });
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <select name="cars" id="cars">
  3. <option value="-">"Please Select"</option>
  4. </select>

请注意,代码部分已经翻译成中文,如有需要,请随时提出其他问题。

英文:

Here is my solution:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. let data = &quot;[[id=1, name=BOTTLE-1], [id=2, name=BOTTLE-2], [id=4, name=BOTTLE-3]]&quot;;
  2. let codingRegex = /id=\d+, name=BOTTLE-\d+/g;
  3. let matches = data.match(codingRegex);
  4. let cars = $(&quot;#cars&quot;);
  5. matches.forEach(match =&gt; {
  6. let temp = match.split(&quot;, &quot;);
  7. let id = temp[0].replace(&quot;id=&quot;, &quot;&quot;),
  8. name = temp[1].replace(&quot;name=&quot;, &quot;&quot;);
  9. let option = new Option();
  10. option.text = name;
  11. option.value = id;
  12. cars.append(option);
  13. });

<!-- language: lang-html -->

  1. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;select name=&quot;cars&quot; id=&quot;cars&quot;&gt;
  3. &lt;option value=&quot;-&quot;&gt;&quot;Please Select&quot;&lt;/option&gt;
  4. &lt;/select&gt;

<!-- end snippet -->

答案2

得分: 0

  1. let data = "[[id=1, name=BOTTLE-1], [id=2, name=BOTTLE-2], [id=4, name=BOTTLE-3]]";
  2. let label = "BOTTLE";
  3. let regExString = "id=\\d+, name=" + label + "-\\d+";
  4. let regExp = new RegExp(regExString, "ig");
  5. let matches = data.match(regExp);
  6. let cars = $("#cars");
  7. matches.forEach(match => {
  8. let temp = match.split(", ");
  9. let id = temp[0].replace("id=", ""),
  10. name = temp[1].replace("name=", "");
  11. let option = new Option();
  12. option.text = name;
  13. option.value = id;
  14. cars.append(option);
  15. });
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <select name="cars" id="cars">
  3. <option value="-">"Please Select"</option>
  4. </select>
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. let data = &quot;[[id=1, name=BOTTLE-1], [id=2, name=BOTTLE-2], [id=4, name=BOTTLE-3]]&quot;;
  2. let label = &quot;BOTTLE&quot;;
  3. let regExString=&quot;id=\\d+, name=&quot;+label+&quot;-\\d+&quot;;
  4. let regExp=new RegExp(regExString,&quot;ig&quot;);
  5. let matches = data.match(regExp);
  6. let cars = $(&quot;#cars&quot;);
  7. matches.forEach(match =&gt; {
  8. let temp = match.split(&quot;, &quot;);
  9. let id = temp[0].replace(&quot;id=&quot;, &quot;&quot;),
  10. name = temp[1].replace(&quot;name=&quot;, &quot;&quot;);
  11. let option = new Option();
  12. option.text = name;
  13. option.value = id;
  14. cars.append(option);
  15. });

<!-- language: lang-html -->

  1. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;select name=&quot;cars&quot; id=&quot;cars&quot;&gt;
  3. &lt;option value=&quot;-&quot;&gt;&quot;Please Select&quot;&lt;/option&gt;
  4. &lt;/select&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月6日 10:46:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定