如何使用按钮在对话框上显示 Javascript 函数的结果

How to show the result of a Javascript Function on a Dialog using a Button?

本文关键字:Javascript 函数 结果 显示 何使用 按钮 对话框      更新时间:2023-09-26

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

泰为你帮忙!

Javascript

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

.HTML

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

演示

function myfunction()
{
  return "It works";  
}
<button onclick="alert(myfunction())">Clic</button>

你可以尝试使用 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>

像这样:

.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/