首页 > 解决方案 > 表单输入文本从 JSON 数组中获取值

问题描述

我为我的 Web 应用程序制作了一个表单,一些字段的值来自从数据库中的处理表中获取的 JSON 数组。

{"treatment":[
     { "id" : 1, "name" : "Leg Training" },
     { "id" : 2, "name" : "Blood Pressure Control" },
     { "id" : 3, "name" : "MTT" },
     { "id" : 4, "name" : "Radiography" },
     { "id" : 5, "name" : "Doppler Ultrasonography" },
]}

我想为我的表单创建输入文本,而不是选择框,以检查用户编写的单词,并从 JSON 数组中的键名中为用户推荐每个相似的单词。当用户从推荐中选择一个单词时,它会在框中显示该单词,但在值中,它将是 id-value 而不是单词 (name-value),因为表格将发布在不同的表格中,至于现在,它只能使用id作为外键从治疗表中获取数据。

标签: javascripthtmljson

解决方案


From what I can understand about your question, this should get you started if you want to use purely HTML and by using input fields.

<form method="get">

  <input name="test" type="text" list="list1" autocomplete="off"/>
    <datalist id="list1">
      <option value="1">Leg Training</option>
      <option value="2">Blood Pressure Control</option>
      <option value="3">MTT</option>
      <option value="4">Radiography</option>
      <option value="5">Doppler Ultrasonography</option>
    </datalist>
    
  <input type="submit">
  
</form>

The Display Text won't really be the name of disease but your users can type the name of the disease to find it from the list.


Let me know if you have any confusion here :)


推荐阅读