简体   繁体   English

Onclick返回确认在Jquery Mobile中不起作用?

[英]Onclick return confirm not working in Jquery Mobile?

I am using the simple javascript confirm code: 我正在使用简单的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>

This usually works except when I use Jquery Mobile the OK and Cancel button both proceeds to the URL. 除了我使用Jquery Mobile时,“ OK和“ Cancel按钮都进入URL,这通常可以工作。 The expected behavior is the Cancel button should not proceed to the URL. 预期的行为是“ Cancel按钮不应进入URL。

ie, I use Jquery Mobile by adding the following in the html head : 即,我通过在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>

Is there anything I should be aware of to make the javascript confirm() function work in Jquery Mobile? 我有什么需要注意的事情,以使javascript confirm()函数在Jquery Mobile中起作用?


Update for Phill Pafford: Phill Pafford更新:

It does work on plain HTML but this is actually what I'm doing and it doesn't work after redirecting to the same page using PHP's header('location:') : 它确实可以在纯HTML上运行,但这实际上是我在做什么,并且在使用PHP的header('location:')重定向到同一页面后,它不起作用:

The page list contains: 页面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>

The list/delete action contains something like this: list/delete操作包含以下内容:

$rest->delete($id);
header('location: http://example.com/list');

As you can see after clicking the URL list/delete/1 the delete procedure will go on then redirect back to the list page. 如您所见,在单击URL list/delete/1 ,删除过程将继续,然后重定向回list页面。 That's where it starts to not work. 那就是开始不起作用的地方。

As soon as the redirection happens the confirm dialog doesn't work anymore. 重定向发生后,确认对话框将不再起作用。

Does something like this work for you? 这样的事情对您有用吗?

JS JS

$('.customClick').on('click', function() {
    var str = $(this).attr('data-str');
    str = (str == '0')? 'Are you sure?' : str;

    return (confirm(str))? true : false;
});

HTML HTML

<a href="http://jsfiddle.net/8CDc5/" class="customClick" data-str="0">Test</a>​

I had the same problem, appears like "return false;" 我遇到了同样的问题,看起来像“ return false”; was not stopping propagation and the link was followed anyway. 没有停止传播,并且仍然遵循链接。 Solved it by explicitly stopping propagation before returning 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>

add this attribute 添加此属性

data-ajax="false" 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM