一次点击即可实现更多功能的问题

Issue with more multiple functions in one onClick

本文关键字:可实现 多功能 问题 一次      更新时间:2023-09-26

>我有两个函数,我需要在一个onClick中执行。第一个是确认提交,如果用户按yes它应该执行第二个功能。它与我不起作用。请帮忙。

在我的代码下面:

<Script>
function checkSubmit(){
if(confirm('Are you sure you want to submit the data?');)
    sendData();
}
</Script>

按钮:

<input type="submit" id="send_data"  class="send_data" value="Send" onclick="checkSubmit()"/>

谢谢大家 ^_^

你说得差不多对。 我认为你的分号在你的 if 语句中把你搞砸了。

看看这个jsFiddle:

function checkSubmit() { 
    if (confirm('Are you sure you want to submit the data?')) 
        sendData(); 
}
function sendData() { alert("data sent"); }

您应该使用标准事件绑定机制:

elem.addEventListener('click', yourFunc, false); // for good browsers
elem.attachEvent('onclick', yourFunc);  // for old IE versions

您可以根据需要添加任意数量的侦听器

以下是参考:

https://developer.mozilla.org/en/DOM/element.addEventListener#Legacy_Internet_Explorer_and_attachEvent

<Script>  
function checkSubmit(){
if(confirm('Are you sure you want to submit the data?'))  //you have small error here
    sendData();
 }
</Script>

只是你的 if 语句中有一个分号。你需要这样的东西:

function checkSubmit() {
    var b = confirm('Are you sure you want to submit the data?');
    if ( b ) {
        sendData();
    } else {
        return false;
    }
}

已编辑:如果要停止提交表单,可以执行以下操作:

<form name="example" action="your url here" method="get" onsubmit="return checkSubmit();">
    <input type="text" name="name" />
    <input type="submit" id="send_data" class="send_data" value="Send" />
</form>