简体   繁体   English

在调用JS函数之前对按钮单击进行验证

[英]Verification on button click before calling JS function

Ok i know how to ask for verification when a button is clicked but how can i make it so that from the same button on click it'll first ask for verification and then do the JS function i want? 好的,我知道单击按钮时如何要求验证,但是我如何才能使它从单击的同一按钮中首先请求验证,然后执行我想要的JS功能?

for example 例如

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>

What i want to do is after the confirmation is given to call the below function, 我要做的是在确认后调用以下函数,

function closer1() {...}

Obviously there is not one button, can i somehow pass the id of the button to the clicked function and somehow call the function from there? 显然没有一个按钮,我能以某种方式将按钮的ID传递给所单击的函数,然后以某种方式从那里调用该函数吗?

What is wrong with 出什么问题了

if(!confirm('Are you sure you want to do this?'))
{
   e.preventDefault();
   return false;
}
if(e=='button1') {
    closer1();
}
var result = confirm("Are you sure you want to do this?");
if (result == false) {
  e.preventDefault(); // user pressed cancel
} else {
  closer1(); // user pressed ok
}

If your clickable buttons share a function, you could implement something like this, where the result varies depending on the id of the button clicked 如果您的可点击按钮共享一个功能,则您可以实施类似的操作,其结果取决于所点击按钮的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
}
function clicked(event, element)
{
   if(!confirm('Are you sure you want to do this?'))
   {
      e.preventDefault();
      return false;
   }

   // you can use 'element' as your element that has been clicked
}

<button type="button" onclick="clicked(event, this)">Close Round 1</button>

Why not just do this? 为什么不这样做呢?

<button type="button" onclick="closer1()">Close Round 1</button>

If thats not what you want to call, then you could do something like this: 如果那不是您想要的电话,那么您可以执行以下操作:

<button type="button" onclick="clicked('button1')">Close Round 1</button>

Javascript 使用Javascript

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

Here's what I would do: 这就是我要做的:


HTML: HTML:

<button type="button" onclick="sample_one(event)">Close Round 1</button>

JS: JS:

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
}

Here is a sample jsfiddle 这是一个示例jsfiddle


function clicked(e){

   e.preventDefault();  

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

    } else { 
    return false;

    }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM