Javascript 单击确认,然后重定向

Javascript 单击确认,然后重定向,javascript,html,onclick,Javascript,Html,Onclick,我正在尝试确认,然后重定向到新页面 如果我在confirm之前删除return,它会以任何方式确认,但如果我保留return它不会重定向到link <button name="payment" class="btn btn-xs-6 btn-danger btn-block" type="button" onclick="return confirm('are you sure you want to cancel?');window.location.href='cancel';

我正在尝试确认,然后重定向到新页面

如果我在
confirm
之前删除
return
,它会以任何方式确认,但如果我保留
return
它不会重定向到link

<button name="payment" class="btn btn-xs-6 btn-danger btn-block" type="button"
    onclick="return confirm('are you sure you want to cancel?');window.location.href='cancel';"
    value="fav_HTML">Cancel Payment
</button>
取消付款

您需要
确认()
的值,该值指示用户是确认还是取消。因此,与其

return confirm('are you sure you want to cancel?'); window.location.href='cancel';
你应该这样做

if (confirm('are you sure you want to cancel?')) window.location.href='cancel';

confirm()
只返回布尔值,因此
if(r==true)
可以替换为
if(r)
@LostMyGlasses,当然了为什么这个问题被否决了?我没有看到任何违反stackoverflow规则的事情。
r = confirm('are you sure you want to cancel?');
if (r == true) {
    window.location.href='cancel';
} else {
   //
}