我正在使用简单的 javascript 确认代码:
function test_confirm (str)
{
str = (str == '0')? 'Are you sure?' : str
return (confirm(str))? true : false
}
<a href="http://example.com" onclick="return test_confirm(0)">Test</a>
这通常有效,除非我使用 Jquery Mobile OK
和Cancel
按钮都会转到 URL。预期的行为是Cancel
按钮不应继续访问该 URL。
即,我通过在 html head
中添加以下内容来使用 Jquery Mobile :
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
制作 javascript confirm()
有什么我应该注意的吗?函数在 Jquery Mobile 中工作吗?
菲尔·帕福德更新:
它确实可以在纯 HTML 上工作,但这实际上是我正在做的事情,并且在使用 PHP 的 header('location:')
重定向到同一页面后它不起作用:
页面list
包含:
<a href="list/delete/1" class="customClick" data-str="0">Delete 1</a>
<a href="list/delete/2" class="customClick" data-str="0">Delete 2</a>
list/delete
操作包含如下内容:
$rest->delete($id);
header('location: http://example.com/list');
点击 URL list/delete/1
后可以看到删除过程将继续,然后重定向回 list
页。这就是它开始不起作用的地方。
一旦发生重定向,确认对话框就不再起作用。
最佳答案
这样的东西对你有用吗?
JS
$('.customClick').on('click', function() {
var str = $(this).attr('data-str');
str = (str == '0')? 'Are you sure?' : str;
return (confirm(str))? true : false;
});
HTML
<a href="http://jsfiddle.net/8CDc5/" class="customClick" data-str="0">Test</a>
关于php - Onclick 返回确认在 Jquery Mobile 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13277910/