How to confirm and call function with onclick

心不动则不痛 提交于 2019-12-07 01:58:36

问题


Can I call JavaScript function with return confirm(); in HTML onclick event or do I need to do function which contains confirmation and call to another function?

<button onclick="return confirm('Are you sure?'); saveandsubmit(event);"></button>

Thanks in advance.


回答1:


Add if condition

<button onclick="if(confirm('Are you sure?')) saveandsubmit(event);"></button>

OR

<button onclick="return confirm('Are you sure?')?saveandsubmit(event):'';"></button>



回答2:


Try below :

<button onclick="confirm('Are you sure ?') && saveAndSubmit(event)">Button</button>


function saveAndSubmit(event){
    alert('saveAndSubmit called !');
}

JSFiddle : https://jsfiddle.net/nikdtu/cyt955bp/



来源:https://stackoverflow.com/questions/32394684/how-to-confirm-and-call-function-with-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!