Javascript 调用JS函数前点击按钮验证

Javascript 调用JS函数前点击按钮验证,javascript,jquery,Javascript,Jquery,好的,我知道当点击按钮时如何请求验证,但是我如何才能使它在点击按钮时,首先请求验证,然后执行我想要的JS功能 比如说 function clicked(e) { if(!confirm('Are you sure you want to do this?'))e.preventDefault(); } <button type="button" onclick="clicked(event)">Close Round 1</button> 显然,没有一个按钮,

好的,我知道当点击按钮时如何请求验证,但是我如何才能使它在点击按钮时,首先请求验证,然后执行我想要的JS功能

比如说

function clicked(e)
{
    if(!confirm('Are you sure you want to do this?'))e.preventDefault();
}

<button type="button" onclick="clicked(event)">Close Round 1</button>
显然,没有一个按钮,我是否可以通过某种方式将按钮的id传递给单击的函数并从那里调用该函数?

有什么问题

if(!confirm('Are you sure you want to do this?'))
{
   e.preventDefault();
   return false;
}
if(e=='button1') {
    closer1();
}
你怎么了

if(!confirm('Are you sure you want to do this?'))
{
   e.preventDefault();
   return false;
}
if(e=='button1') {
    closer1();
}
如果您的可点击按钮共享一个功能,那么您可以实现类似的功能,其结果取决于所点击按钮的id

var result = confirm("Are you sure you want to do this?");
if (result == false) {
  e.preventDefault(); // user pressed cancel
} else if ($(this).attr('id') == 'button1') {
  closer1(); // user pressed ok, and request came from button 1
} else if ($(this).attr('id') == 'button2') {
  closer2(); // user pressed ok, and request came from button 2
}
如果您的可点击按钮共享一个功能,那么您可以实现类似的功能,其结果取决于所点击按钮的id

var result = confirm("Are you sure you want to do this?");
if (result == false) {
  e.preventDefault(); // user pressed cancel
} else if ($(this).attr('id') == 'button1') {
  closer1(); // user pressed ok, and request came from button 1
} else if ($(this).attr('id') == 'button2') {
  closer2(); // user pressed ok, and request came from button 2
}
单击的函数(事件、元素)
{
如果(!confirm('您确定要执行此操作吗?'))
{
e、 预防默认值();
返回false;
}
//您可以使用“元素”作为已单击的元素
}
结束第一轮
单击功能(事件、元素)
{
如果(!confirm('您确定要执行此操作吗?'))
{
e、 预防默认值();
返回false;
}
//您可以使用“元素”作为已单击的元素
}
结束第一轮
为什么不这样做呢

结束第1轮

如果这不是你想说的,那么你可以这样做:

结束第1轮

Javascript

function clicked(str){
   if(str=='button1')
      closer1();
   if(str=='button2')
      closer2();
   //and so on
}
为什么不这样做呢

结束第1轮

如果这不是你想说的,那么你可以这样做:

结束第1轮

Javascript

function clicked(str){
   if(str=='button1')
      closer1();
   if(str=='button2')
      closer2();
   //and so on
}

下面是我要做的:


HTML:


这是一个示例



下面是我要做的:


HTML:


这是一个示例


function clicked(e){

   e.preventDefault();  

    var r = confirm("Are you sure you want to delete?");
    if (r == true) {

    } else { 
    return false;

    }

    }
<button type="button" onclick="sample_one(event)">Close Round 1</button>
function sample_one(e)
{
    e.preventDefault(); // Stops default action

    // Ask for confirmation
    if ( confirm('Are you sure you want to do this?') )
    {
        sample_two(); // Call the second defined function
    }
}

function sample_two()
{
    alert('sample_two function called.'); // Alert message to show defined function call
}