[英]How to submit form when any value selected from populated textbox using javascript or jQuery without clicking submit button
搜索.php
在這種形式中,當我們在文本框中輸入內容時,會從api_search.php
頁面中獲取匹配的單詞,並顯示在所附屏幕截圖中。
<form role="form" id="frm_search" name="frm_search" method="POST" action="./api_search_p.php" enctype="multipart/form-data" >
<script type="text/javascript">
$(function()
{
$( "#txt_itemname" ).autocomplete({
source: '../user/api_search.php'
});
});
</script>
<input type="text" id="txt_itemname" name="txt_itemname" class="form-control" required data-validation-required-message="Please enter something here">
<button type="submit" class="btn btn-warning"><i class='fas fa-search'></i> </button></span>
</form>
api_search.php
search.php
上的文本框是根據此頁面的 API 結果數據填充的。
-- API data are coming using curl in $result array --
$json = json_decode($result, true);
$arr_searchTerm = array();
if (is_array($json) && !empty($json))
{
foreach ($json as $key1 => $level1)
{
if (is_array($level1) && !empty($level1))
{
foreach ($level1 as $key2 => $level2)
{
array_push($arr_searchTerm, $level2['TITLE']);
}
}
}
}
echo json_encode($arr_searchTerm);
目前需要點擊提交按鈕來提交表單。 But I want to do so when any word from fetched result is selected / clicked then form should be submitted immediately without clicking submit button.
我用文本框嘗試onselect="this.form.submit()"
& onchange="this.form.submit()"
但沒有在任何 javascript 事件上提交表單。
請讓我知道如何使它按預期工作。
截屏
你可以試試這個方法。 在這種情況下,您可以在每次有人插入某些東西時調用 API。 這意味着每封信都會打電話
document.querySelector("#txt_itemname").addEventListener('input',(e)=>{
...
})
此外,如果您將其作為 API 進行操作,請嘗試使用 PHP 來查看http_responses
您可以在代碼中使用 select function
$( "#txt_itemname" ).autocomplete({
source: '../user/api_search.php',
select: function(e, ui){
this.value = ui.item.value;
this.form.submit();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.