如何在“属性事件”中获取onclick“属性事件”返回值?

时间:2009-12-29 18:30:09

标签: javascript jquery javascript-events

考虑以下代码示例:

<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的情况下获得此返回值

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

4 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分: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.
    });
});

答案 2 :(得分:1)

你需要明确添加单词“return”,如下所示:

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>

答案 3 :(得分:-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 }); });