如何将JavaScript确认为HTML5按钮操作?
我有以下代码:
if(changedStatus()){
var mAns = confirm("You have changed your status. This will alter your estate plan and may cause you to lose some data. Are you sure that you want to do this?");
if(!mAns) return false;
}
这与默认的JS确认弹出窗口非常有用,但是我想用HTML5按钮实现相同的操作:
<div id="cres_statusmssg">
<p>Changing your status will alter your estate plan and may cause you to lose some data. Do you want to proceed?</p>
<button id="cres_yes">Yes</button>
<button id="cres_cancel">Cancel</button>
我已经将单击事件应用于两个按钮,但是我无法让它们上班。有什么想法吗?
I have the following code:
if(changedStatus()){
var mAns = confirm("You have changed your status. This will alter your estate plan and may cause you to lose some data. Are you sure that you want to do this?");
if(!mAns) return false;
}
This works great with the default JS confirm pop-up, but I would like to implement the same action with HTML5 buttons instead:
<div id="cres_statusmssg">
<p>Changing your status will alter your estate plan and may cause you to lose some data. Do you want to proceed?</p>
<button id="cres_yes">Yes</button>
<button id="cres_cancel">Cancel</button>
I have applied click events to both buttons, but I can't get them to work. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要了解如何为异步而写作,因为没有其他方法可以实现您想做的事情。内置的
确认
(提示>提示> ,arter> arter
et)有效地暂停JS执行 - up,您没有那么奢侈。您需要使用回调来实现一个工作解决方案,
我建议使用诺言 - 无论如何,这只是荣耀的回调,还可以将能够与
async /
等待
- 这具有使代码 LOW 更像您使用的代码的优点,如果您没有做太多的异步编码在以上,对话框承诺总是可以解决的,但是还有其他选择,如果按下取消
You'll need to understand how to write for asynchrony, since there is no other way to achieve what you want to do. The built-in
confirm
(andprompt
,alert
etc) effectively pause JS execution until the user has interacted with the pop-upWriting nicer pop-ups, you don't have that luxury. You'll need to use callbacks to achieve a working solution
I'd recommend using Promises - which are just glorified callbacks anyway, with the added advantage of being able to be used with
async
/await
- this has the advantage of making the code look more like the code you're used to if you haven't done much asynchronous codingIn the above, the dialog promise always resolves, but there is an alternative, to reject if cancel is pressed
尝试使用bootstrap模态
使用Twitter Bootstrap Modal而不是确认Dialog
或jQuery ui
对话?
Try to use Bootstrap Modal
Using Twitter bootstrap modal instead of confirm dialog
Or jquery ui
How to implement "confirmation" dialog in Jquery UI dialog?