我试图在单击表单提交按钮时触发一个功能。我尝试了点击和提交方法,但没有触发。任何建议都会受到高度赞赏。
<form id="my_form" action="process.php" method="POST">
<input type="submit" id="subm" class=" submit_button" value="Submit" >
</form>
$("#subm").click(function() {
alert('Doesnt work');
}
$("#my_form").submit(function() {
alert('Doesnt work');
}
答案 0 :(得分:1)
首先,你需要添加Jquery库才能工作,然后你必须使用preventDefault()
,如下所示: -
<form id="my_form" action="process.php" method="POST">
<input type="submit" id="subm" class=" submit_button" value="Submit" >
</form>
<script src = "https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type = "text/javascript">
$(document).ready(function(){ // After document is completly loaded
$("#subm").click(function(e) {
e.preventDefault(); // prevent the form submit on click
alert('Does work'); //alert the message
});// you missed ); here
});
</script>
注意: - 检查您的浏览器开发控制台,您将确切地知道存在哪些错误。
$(document).ready(function(){ // After document is completly loaded
$("#subm").click(function(e) {
e.preventDefault(); // prevent the form submit on click
alert('Does work'); //alert the message
});// you missed ); here
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="my_form" action="process.php" method="POST">
<input type="submit" id="subm" class=" submit_button" value="Submit" >
</form>
&#13;