javascript - How to capture choice from a list nested with a radio button
问题描述
I am loading an HTML page which dynamically populates the result of a search (for a book) in a set of lists attached to a radio button. The user is to choose which book in particular they are looking for from the set of lists by selecting the radio button and clicking "Submit".
How do I capture the ISBN from the list for the radio button that the user selects? Code below.
<form id="user_choice" action="/search" method="post">
{% for data in data %}
<div class="form-group">
<input type="radio" value="book_choice" id="choice" onClick="userSelection(document.getElementById('choice').value">
<ul>
<li> ISBN: {{ data[0] }} </li>
<li> Title: {{ data[1] }} </li>
<li> Author: {{ data[2] }} </li>
<li> Year: {{ data[3] }} </li>
</ul>
</div>
<br>
{% endfor %}
<button class="btn btn-primary" onClick="submitUserSelection()">Select</button>
</form>
解决方案
由于您已经在点击时获取了收音机的值,您可以将值属性更改为书籍 ISBN。前任:
<form id="user_choice" action="/search" method="post">
{% for data in data %}
<div class="form-group">
<input type="radio" value="{{ data[0] }}" onClick="userSelection(this.value)">
<ul>
<li>ISBN: {{ data[0] }}</li>
<li>Title: {{ data[1] }}</li>
<li>Author: {{ data[2] }}</li>
<li>Year: {{ data[3] }}</li>
</ul>
</div>
<br>
{% endfor %}
<button class="btn btn-primary" onClick="submitUserSelection()">Select</button>
</form>
我还删除了重复的 id,因为您永远不想拥有重复的 id
推荐阅读
- regex - sed:替换与包含正斜杠的模式匹配的行?
- java - 当我通过 Java 执行时,VBScript 需要很长时间/不执行,但是当我通过命令提示符执行它时它会运行?
- mysql - 选择不同的行,将它们转换为列并计算值
- rest - 使用 OData 创建异步资源
- python - Pandas .at 抛出 ValueError:基于整数索引的索引只能有整数索引器
- pyspark - pyspark:read.parquet 同时跳过丢失的文件
- android - 在 android 应用程序中显示特定活动的数据
- ruby-on-rails - 警告:命令行选项“-Wimplicit-int”对 C/ObjC 有效,但对 C++ 无效
- kubernetes - 负载均衡器后面的请求
- php - fgetcsv 错误地解释数字