空数组:
<script type="text/javascript">
var foodList=[];
</script>
php形式:
<form id="foodForm" action="" method="POST">
<div class=".col-md-6">
<div id="scrollable-dropdown-menu">
<input class="typeahead" type="text" name="search" placeholder="Foods"> </div>
<input type="button" class="submit_btn" value="ADD" name="add" onclick="functionAdd(); reset();" />
<input type="button" class="submit_btn" value="DONE" name="done" onclick="functionDone()" />
</div>
Javascript:
<script>
form=document.getElementById("foodForm");
function functionAdd() {
foodList.push(document.getElementsByName('search'));
form.submit();
}
function functionDone() {
form.action="submit_for_done.php";
form.submit();
}
functionAdd()无法正常工作。我希望函数将表单中的搜索结果添加到空数组( foodList )中。我该如何解决?
答案 0 :(得分:0)
请更新您的functionAdd
函数以找到值<input name="search">
:
<script>
var foodList = [];
function functionAdd () {
var searchEl = document.getElementsByName('search')[0]; // input search element
if (searchEl.value && searchEl.value.length > 0) { // exclude adding of empty values
foodList.push(searchEl.value);
}
// form.submit(); // I think you don't need to submit the form every time you add new item to the food list
}
function functionDone() {
document.write(foodList);
form.action="submit_for_done.php";
form.submit();
}
</script>