2009-12-29 81 views
1

考虑这个代码示例:如何在“属性事件”中获得onclick“属性事件”返回值?

<a href="some url" onclick="confirm('ok to proceed ?')">bla</a>` 

<script type="text/javascript"> 
    $(document).ready(function() { 
       $("a").live("click", function (event) { 
        // In that function, I want to get 
        // the "confirm" return value     
       }); 
</script> 

它可以在不修改DOM得到这个返回值?

感谢.............

+0

由于下面的答案,但我的问题恰恰是属性事件(的onclick)和物业组合事件(onClick),而不修改明确的onclick属性: 查看我的jquery插件:http://garage.pimentech.net/scripts_doc_jquery_jframe/ >>>>>如果onclick属性返回false,我想停止事件传播。 – fredz 2009-12-29 20:37:03

回答

1

,你需要明确加上 “回报”,像这样:

onclick="return confirm('ok to proceed?')" 

编辑:我最初的回答是BS ,这是我最后的工作。

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <script type="text/javascript" src="jquery-1.3.2.js"></script> 
    <script type="text/javascript"> 
$(document).ready(function(){ 
    $("a").click(function() { 
     var stuff = confirm('ok?'); 
     if (stuff) { 
     alert('was ok, stuff=' + stuff); 
     } 
    else { 
     alert('was not ok, stuff=' + stuff); 
     } 
    }); 
}); 
    </script> 
    </head> 
    <body> 
    <a href="http://jquery.com/">jQuery</a> 
    </body> 
</html> 
+0

嗯,也许这就是它。返回值存储在哪里?传递给匿名函数的jQuery'event'参数? – 2009-12-29 18:32:21

+0

如果用户单击取消,iirc事件将被取消,所以我不确定JQuery代码是否会执行。 – 2009-12-29 18:40:18

2
<a href="some url" onclick="var myVar = confirm('ok to proceed ?')">bla</a> 

如果我没有记错,这应该工作。然后,您可以在您的jQuery块中访问myVar,就像您可以访问任何其他Javascript变量一样。

2

不,该值不会存储在任何地方供您访问。获得该值的唯一方法是将其移动到一个jQuery处理这样的单击事件:

$(document).ready(function() { 
    // The removeAttr removes the original "onclick" attribute 
    $("a").removeAttr('onclick').live("click", function (event) { 
     var ret = confirm('ok to proceed ?'); 
     // Do what you want here because `ret` has the return 
     // value from the `confirm` call. 
    }); 
}); 
-1

getValueConfirm`

var isConfirm = false; function confirmDialog(){ isconfirm = confirm('ok to proceed ?'); } $(document).ready(function() { $("a").live("click", function (event) { alert(isconfirm);// your value that you want get it }); });