2012-02-17 92 views
468

我将制作一个按钮来执行操作并将数据保存到数据库中。如何使用“是”和“否”选项创建对话框?

一旦用户点击按钮,我想要一个JavaScript警报提供“是”和“取消”选项。如果用户选择“是”,则数据将被插入到数据库中,否则不会采取任何行动。

如何显示这样的对话框?

+35

这是在谷歌我的查询和回答的第一个结果是正是我一直在后......这个问题可能不会在最清晰的表述方式但讽刺是有点过度,不是吗? – Elliott 2012-09-05 08:34:51

+3

@Elliott看来有些评论已被删除。你记得讽刺的是什么? – 2014-02-04 09:51:11

+0

我不记得我很害怕:/ – Elliott 2014-02-04 10:44:40

回答

904

你可能寻找confirm(),它显示一个提示,并返回true或基于用户决定什么false

if (confirm('Are you sure you want to save this thing into the database?')) { 
    // Save it! 
} else { 
    // Do nothing! 
} 
+0

谢谢我以后再试:D – crchin 2012-02-17 20:21:55

+73

确认OK和CANCEL按钮。这些按钮可以设置为YES/NO吗? – Owen 2013-04-19 00:20:44

+11

@Owen号[该规范(http://www.w3.org/html/wg/drafts/html/master/webappapis.html#simple-dialogs)说,你只能提供一个消息。你可以在HTML中模拟一个对话框(尽管它不会像内置的那样阻塞)。 [jQuery Dialog](http://jqueryui.com/dialog/#modal-confirmation)是实现这种事情的一个很好的例子。 – s4y 2013-04-19 17:00:41

5

您可以拦截JS的onSubmit事件。然后调用确认提醒,然后获取结果。

+1

“oncomit”?你的意思是“提交”吗? – nnnnnn 2012-02-17 20:25:50

+0

你是完全正确的。 – 2012-02-17 20:28:43

59
var answer = confirm("Save data?") 
if (answer) { 
    //some code 
} 
else { 
    //some code 
} 

使用confirm,而不是警告。这是实现该功能的最简单方法。

+10

你也可以用'if(confirm(“...”)){'代替 – 2014-03-06 18:06:17

53

如何使用要做到这一点“内联”的JavaScript:

<form action="http://www.google.com/search"> 
    <input type="text" name="q" /> 
    <input type="submit" value="Go" 
    onclick="return confirm('Are you sure you want to search Google?')" 
    /> 
</form> 
+2

这是更好地处理表单的onsubmit事件:与您的代码,如果用户按下文本输入,表单被提交,没有任何要求! – p91paul 2013-06-03 13:58:36

+1

@ p91paul - 哪一款浏览器会失败?我只是尝试在Windows上按IE,Chrome和Safari,并按预期工作。 http://jsfiddle.net/ALjge/1/ – dana 2013-06-03 16:24:02

+0

嗯,我确定我在说什么,但是我没有测试过,而且我错了。抱歉! – p91paul 2013-06-03 21:41:49

30

避免内联JavaScript - 更改行为将意味着编辑代码的每个实例,并且它不美观!

更简洁的方法是在元素上使用数据属性,如data-confirm="Your message here"。下面我的代码支持下列操作,包括动态生成的元素:

  • abutton点击
  • form提交
  • option选择

的jQuery:

$(document).on('click', ':not(form)[data-confirm]', function(e){ 
    if(!confirm($(this).data('confirm'))){ 
     e.stopImmediatePropagation(); 
     e.preventDefault(); 
    } 
}); 

$(document).on('submit', 'form[data-confirm]', function(e){ 
    if(!confirm($(this).data('confirm'))){ 
     e.stopImmediatePropagation(); 
     e.preventDefault(); 
    } 
}); 

$(document).on('input', 'select', function(e){ 
    var msg = $(this).children('option:selected').data('confirm'); 
    if(msg != undefined && !confirm(msg)){ 
     $(this)[0].selectedIndex = 0; 
    } 
}); 

HTML :

<!-- hyperlink example --> 
<a href="http://www.example.com" data-confirm="Are you sure you want to load this URL?">Anchor</a> 

<!-- button example --> 
<button type="button" data-confirm="Are you sure you want to click the button?">Button</button> 

<!-- form example --> 
<form action="http://www.example.com" data-confirm="Are you sure you want to submit the form?"> 
    <button type="submit">Submit</button> 
</form> 

<!-- select option example --> 
<select> 
    <option>Select an option:</option> 
    <option data-confirm="Are you want to select this option?">Here</option> 
</select> 

JSFiddle demo

+1

非常干净的解决方案我从来没有想过。甚至可以更简洁,但:'$( “[数据确认]”)对( '点击,提交',函数(){/ * ... * /})' – 2013-08-22 08:38:02

+0

对不起,忍不住有再看一遍。首先:事件应该由一个空格分开。第二:你仍然可以收紧代码http://jsfiddle.net/jguEg/;) – 2013-08-22 12:27:53

+0

@GrimaceofDespair我已经更新了代码,因为点击并确认一个'type =“按钮”'然后询问用户是否想要提交窗体(因为你正在点击一个窗体元素),在再次单击确定后显然没有发生。 – rybo111 2014-03-29 14:31:37

11

你必须创建custome confirmBox,这是不可能改变被确认函数在对话框中显示的按钮。

Jquery的confirmBox


看到这个例子:https://jsfiddle.net/kevalbhatt18/6uauqLn6/

<div id="confirmBox"> 
    <div class="message"></div> 
    <span class="yes">Yes</span> 
    <span class="no">No</span> 
</div> 

function doConfirm(msg, yesFn, noFn) 
{ 
    var confirmBox = $("#confirmBox"); 
    confirmBox.find(".message").text(msg); 
    confirmBox.find(".yes,.no").unbind().click(function() 
    { 
     confirmBox.hide(); 
    }); 
    confirmBox.find(".yes").click(yesFn); 
    confirmBox.find(".no").click(noFn); 
    confirmBox.show(); 
} 

您的代码调用它:

doConfirm("Are you sure?", function yes() 
{ 
    form.submit(); 
}, function no() 
{ 
    // do nothing 
}); 

**纯JavaScript confirmBox **


http://jsfiddle.net/kevalbhatt18/qwkzw3rg/127/

<div id="id_confrmdiv">confirmation 
    <button id="id_truebtn">Yes</button> 
    <button id="id_falsebtn">No</button> 
</div> 

<button onclick="doSomething()">submit</button> 

脚本

<script> 

function doSomething(){ 
document.getElementById('id_confrmdiv').style.display="block"; //this is the replace of this line 


document.getElementById('id_truebtn').onclick = function(){ 
    //do your delete operation 
    alert('true'); 
}; 

document.getElementById('id_falsebtn').onclick = function(){ 
    alert('false'); 
    return false; 
}; 
} 
</script> 

CSS

body { font-family: sans-serif; } 
#id_confrmdiv 
{ 
    display: none; 
    background-color: #eee; 
    border-radius: 5px; 
    border: 1px solid #aaa; 
    position: fixed; 
    width: 300px; 
    left: 50%; 
    margin-left: -150px; 
    padding: 6px 8px 8px; 
    box-sizing: border-box; 
    text-align: center; 
} 
#id_confrmdiv button { 
    background-color: #ccc; 
    display: inline-block; 
    border-radius: 3px; 
    border: 1px solid #aaa; 
    padding: 2px; 
    text-align: center; 
    width: 80px; 
    cursor: pointer; 
} 
#id_confrmdiv .button:hover 
{ 
    background-color: #ddd; 
} 
#confirmBox .message 
{ 
    text-align: left; 
    margin-bottom: 8px; 
} 

+1

太好了..很简单,纯粹的JavaScript和没有那么多的CSS。喜欢它:D – erm3nda 2015-08-03 10:56:29

+0

确认框应该消失后,一个项目被按下。此外,你应该使用类,而不是ID。 – rybo111 2015-12-27 11:47:47

+0

@rogerdpack我更新提琴让我知道如果需要从我身边任何东西:) – 2016-10-20 06:24:58

8

这个插件可以他lp you jquery-confirm易于使用

$.confirm({ 
title: 'Confirm!', 
content: 'Simple confirm!', 
confirm: function(){ 
    alert('Confirmed!'); 
}, 
cancel: function(){ 
    alert('Canceled!') 
} 

});

3

另一种方式来做到这一点:

$("input[name='savedata']").click(function(e){ 
     var r = confirm("Are you sure you want to save now?"); 

     //cancel clicked : stop button default action 
     if (r === false) { 
      return false; 
     } 

     //action continues, saves in database, no need for more code 


    }); 
-4
document.getElementById("button").addEventListener("click", function(e) { 
    var cevap = window.confirm("Satın almak istediğinizden emin misiniz?"); 
    if (cevap) { 
    location.href='Http://www.evdenevenakliyat.net.tr';  
    } 
}); 
相关问题