将 onclick 函数添加到提交按钮 [英] add onclick function to a submit button

查看:15
本文介绍了将 onclick 函数添加到提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习 javascript 和 php.我创建了一个联系表单,我希望提交按钮在按下它时完成两件事:

I'm just learning javascript and php. I created a contact form and I'd like the submit button to accomplish two things when I press it:

  1. 将数据提交给我(这部分正在运行)
  2. 阅读我的 onclick 功能(这部分不起作用)

<input id="submit" name="submit" type="submit" value="Submit" onclick="eatFood()">

<?php
if ($_POST['submit']) {
 ////????? 
}
?>

我正在将数据发送到我的电子邮件,所以我明白了.但是 onclick 功能似乎不起作用.我尝试查看 为提交按钮添加 onclick 功能,但没有帮助.

I'm sending the data to my email, and so I get that. But the onclick function doesn't seem to work. I tried reviewing add onclick function for submit button but it didn't help.

推荐答案

我需要查看您的提交按钮 html 标签以获得更好的帮助.我不熟悉 php 以及它如何处理回发,但我想这取决于你想要做什么,你有三个选择:

I need to see your submit button html tag for better help. I am not familiar with php and how it handles the postback, but I guess depending on what you want to do, you have three options:

  1. 在客户端获取处理onclick按钮:在这种情况下你只需要调用一个javascript函数.
  1. Getting the handling onclick button on the client-side: In this case you only need to call a javascript function.

function foo() {
   alert("Submit button clicked!");
   return true;
}

<input type="submit" value="submit" onclick="return foo();" />

  1. 如果要在服务器端处理点击,首先要确保表单标签方法属性设置为post:

<form method="post">

  • 您可以使用 form 本身的 onsubmit 事件将您的函数绑定到它.

  • You can use onsubmit event from form itself to bind your function to it.

    <form name="frm1" method="post" onsubmit="return greeting()">
        <input type="text" name="fname">
        <input type="submit" value="Submit">
    </form>

    这篇关于将 onclick 函数添加到提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆