我将制作一个按钮来执行操作并将数据保存到数据库中。如何使用“是”和“否”选项创建对话框?
一旦用户点击按钮,我想要一个JavaScript警报提供“是”和“取消”选项。如果用户选择“是”,则数据将被插入到数据库中,否则不会采取任何行动。
如何显示这样的对话框?
我将制作一个按钮来执行操作并将数据保存到数据库中。如何使用“是”和“否”选项创建对话框?
一旦用户点击按钮,我想要一个JavaScript警报提供“是”和“取消”选项。如果用户选择“是”,则数据将被插入到数据库中,否则不会采取任何行动。
如何显示这样的对话框?
你可能寻找confirm()
,它显示一个提示,并返回true
或基于用户决定什么false
:
if (confirm('Are you sure you want to save this thing into the database?')) {
// Save it!
} else {
// Do nothing!
}
谢谢我以后再试:D – crchin 2012-02-17 20:21:55
确认OK和CANCEL按钮。这些按钮可以设置为YES/NO吗? – Owen 2013-04-19 00:20:44
@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
您可以拦截JS的onSubmit
事件。然后调用确认提醒,然后获取结果。
“oncomit”?你的意思是“提交”吗? – nnnnnn 2012-02-17 20:25:50
你是完全正确的。 – 2012-02-17 20:28:43
var answer = confirm("Save data?")
if (answer) {
//some code
}
else {
//some code
}
使用confirm
,而不是警告。这是实现该功能的最简单方法。
你也可以用'if(confirm(“...”)){'代替 – 2014-03-06 18:06:17
如何使用要做到这一点“内联”的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>
避免内联JavaScript - 更改行为将意味着编辑代码的每个实例,并且它不美观!
更简洁的方法是在元素上使用数据属性,如data-confirm="Your message here"
。下面我的代码支持下列操作,包括动态生成的元素:
a
和button
点击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>
非常干净的解决方案我从来没有想过。甚至可以更简洁,但:'$( “[数据确认]”)对( '点击,提交',函数(){/ * ... * /})' – 2013-08-22 08:38:02
对不起,忍不住有再看一遍。首先:事件应该由一个空格分开。第二:你仍然可以收紧代码http://jsfiddle.net/jguEg/;) – 2013-08-22 12:27:53
@GrimaceofDespair我已经更新了代码,因为点击并确认一个'type =“按钮”'然后询问用户是否想要提交窗体(因为你正在点击一个窗体元素),在再次单击确定后显然没有发生。 – rybo111 2014-03-29 14:31:37
你必须创建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;
}
这个插件可以他lp you jquery-confirm易于使用
$.confirm({
title: 'Confirm!',
content: 'Simple confirm!',
confirm: function(){
alert('Confirmed!');
},
cancel: function(){
alert('Canceled!')
}
});
另一种方式来做到这一点:
$("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
});
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';
}
});
这是在谷歌我的查询和回答的第一个结果是正是我一直在后......这个问题可能不会在最清晰的表述方式但讽刺是有点过度,不是吗? – Elliott 2012-09-05 08:34:51
@Elliott看来有些评论已被删除。你记得讽刺的是什么? – 2014-02-04 09:51:11
我不记得我很害怕:/ – Elliott 2014-02-04 10:44:40