[英]How to I call a function when form submit
以下是我在html頁面中的部分,該部分將獲取閾值並使用該值調用函數
<form id="distance_input" onSubmit="return false;" >
<p>Enter a threshold</p>
<div class="col-md-8" style="display: inline-block; left:-30px">
<div class="col-md-4">
<div class="input-group">
<input type="text" class="form-control" id="usr" class="search" onkeydown="search(this)"> </input>
<span class="input-group-addon" style="width: 50px">km</span>
</div>
</div>
<div class="col-md-4" style="padding-left:0px; margin-left:-10px">
<input type="button" class="btn btn-success" value="Submit" id="myButton"> </input>
</div>
</div>
<p> </p>
</form>
<script type="text/javascript">
$('#myButton').click(function(){
var value = $("input[type=text]").val();
console.log(value+ " from the submit button");
set_to_threshold(value);
});
function search(ele) {
if(event.keyCode == 13) {
console.log(ele.value+" from enter button");
set_tothreshold(ele.value);
}
}
</script>
但是當我這樣做時,我不會刷新圖(set_tothreshold函數在值傳遞到函數中時獲取圖的新數據)並顯示
Uncaught ReferenceError: search is not defined
at HTMLInputElement.onkeydown
當我嘗試
<script type="text/javascript">
$('#myButton').click(function(){
var value = $("input[type=text]").val();
console.log(value+ " from the submit button");
set_to_threshold(value);
});
</script>
當我按下提交按鈕時也沒有發生任何變化(即使在控制台中也不打印值)。但是為什么不打印該值。感謝您的幫助。
這就是你想要的。 表單提交時的yourFunction調用
<form id="distance_input" onSubmit="yourFunction(); return false;" >
$('#myButton').click(function(){ var value = $("input[type=text]").val(); console.log(value); set_to_threshold(value); }); function set_to_threshold(val){ console.log('do something here with val ', val); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <input type="text" value="input something"/> <button id="myButton">click</button> </body> </html>
請在文件中包含jquery。 工作正常。
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
將其包括在html頁面的開頭部分。 此外,您還引用了未定義的函數set_to_threshold()
。 因此,只有您遇到了錯誤。 請為此編寫一個函數。 會的!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.