[英]Onclick return confirm not working in Jquery Mobile?
我正在使用簡單的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中起作用?
Phill Pafford更新:
它確實可以在純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>
我遇到了同樣的問題,看起來像“ return false”; 沒有停止傳播,並且仍然遵循鏈接。 通過在返回false之前顯式停止傳播來解決該問題。
function test_confirm (e, str)
{
str = (str == '0')? 'Are you sure?' : str
if (!confirm(str)) {
e.stopPropagation();
return false;
}
return true;
}
<a href="http://example.com" onclick="var e=arguments[0] || window.event; return test_confirm(e,0)">Test</a>
添加此屬性
data-ajax =“ false”
<a data-ajax="false" href="http://example.com" onclick="return test_confirm(0)">Test</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.