使用jquery从文本文件创建的选择下拉列表中捕获所选项目的值 [英] capture selected item value from a select drop down list created from a text file using jquery
问题描述
我有一个文本文件,其中包含一些简单的单词,例如:make,this,work.
I have a text file that contains a few simple words like: make, this, work.
我希望能够加载文本文件并从中创建一个动态选择下拉列表.当用户单击提交时,它会打印出所选项目的值.
I want to be able to load the text file and create a dynamic select drop down list from it. When the user clicks submit, it prints out the value of selected item.
下面是代码,我使用把手将页面呈现给用户.当用户单击提交"按钮时,它将转到索引/显示以查看他们选择的内容.
Below is the code, I use handle bar to render the page to users. When the user clicks on the submit button, it goes to index/display to see what they selected.
不是打印出所选选项的实际值,而是打印出所选选项的索引,而不是值.
Instead of printing out the actual value of selected options, it prints out index of the selected option, not the value.
<form method="post" action="index/display" id = "first" name="first">
<fieldset>
<div style="display: inline-block; margin-left:10px ">
<legend>Selecting items from text file</legend>
<script type="text/javascript" src="js/jquery.js"></script>
<script>
$.get("js/pytxt.txt",
function(data) {
/*alert( "Data Loaded: " );*/
var options = data.split(','),
$select = $('select#value');
for (var i = 0; i < options.length; i++) {
$select.append('<option value="' + i + '">' + options[i] + '</option>"');
console.log ($select)};
});
</script>
<p>
<label></label>
<select id = "value" name="first">
<option selected value="base">Please Select</option>
</select>
</p>
</div>
<p>
<input type="submit">
</p>
</fieldset>
这是发布功能:
app.post('/index/display', (req, res) => {
res.status(200).send('POST works!' + req.body.first);
});
推荐答案
请替换:
$select.append('<option value="' + i + '">' + options[i] + '</option>"');
此行:
$select.append('<option value="' + options[i] + '">' + options[i] + '</option>"');
这应该可以解决您的问题.
This should fix your problem.
这篇关于使用jquery从文本文件创建的选择下拉列表中捕获所选项目的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!