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

查看:96
本文介绍了将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天全站免登陆