【问题标题】:How to show the result of a Javascript Function on a Dialog using a Button?如何使用按钮在对话框上显示 Javascript 函数的结果?
【发布时间】:2016-04-27 11:38:48
【问题描述】:

我的问题是如何使用按钮在对话框上显示 Javascript 函数的结果?

你帮帮忙!

【问题讨论】:

    标签: javascript function button


    【解决方案1】:

    Javascript

    function yourfunction()
    {
    // code
    return result;
    }
    

    HTML

    <button onclick="alert(yourfunction());">Click</button>
    

    演示

    function myfunction()
    {
      return "It works";  
    }
    &lt;button onclick="alert(myfunction())"&gt;Clic&lt;/button&gt;

    【讨论】:

      【解决方案2】:

      您可以尝试使用 jQuery UI 对话框:

      function someFunction() {
        return 42;
      }
      
      $(function() {
        $('<div>' + someFunction() + '</div>').appendTo('body').dialog();
      });
      <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

      【讨论】:

        【解决方案3】:

        像这样:

        HTML

        <button id="button">
            Show results
        </button>
        

        JS

        // The function whose results we want to print out
        function foo() {
            var result = "bar";
            // we need to return the result
            return result;
        }
        
        // get the button in the HTML
        var button = document.getElementById("button");
        
        // attach the click event on it
        button.addEventListener("click", function() {
           // after clicking the button, show the results
           alert(foo());
        });
        

        https://jsfiddle.net/v39honrp/2/

        【讨论】:

          猜你喜欢
          • 2020-05-12
          • 1970-01-01
          • 1970-01-01
          • 2021-12-14
          • 2012-01-23
          • 1970-01-01
          • 1970-01-01
          • 2020-04-12
          • 1970-01-01
          相关资源
          最近更新 更多