繁体   English   中英

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

[英]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.

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