簡體   English   中英

使用表單提交值調用 JS Function

[英]Call JS Function with Form Submit Value

我有一個自動完成表單輸入,我想用自動完成選擇的值調用 javascript function。

我有表格和JS function,如果我給出一個特定的值,它就會調用js function。 但是我沒有得到如何將表單提交的值傳遞給js function。 這是我嘗試過的代碼。 如果您知道如何傳遞這些值,請告訴我。 非常感謝您的幫助。 謝謝。

<form autocomplete="off" action="/action_page.php">
  <div class="autocomplete" style="width:1120px;">
    <input id="myInput" type="text" name="myCountry" placeholder="country">
  </div>
  <input onclick="filterSelection('all')" type="submit"> <!-- filterSelection() is the JS function, and I am passing value as 'all'. its working. But I want to pass the submit value from the form.
</form>

您可以使用document.getElementById('myInput').value傳遞元素的當前值:

演示:

 function filterSelection(v){ alert(v); }
 <form autocomplete="off" action="/action_page.php"> <div class="autocomplete" style="width:1120px;"> <input id="myInput" type="text" name="myCountry" placeholder="country"> </div> <input onclick="filterSelection(document.getElementById('myInput').value)" type="submit"> <,-- filterSelection() is the JS function. and I am passing value as 'all'. its working. But I want to pass the submit value from the form.--> </form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM