在警告框中打印功能的结果

时间:2015-04-25 23:05:31

标签: javascript jquery html

所以我有一个函数,我们称之为calculate()。这会从某些表单中提取数据,并在检查或填写项目时将其添加到一起并将它们添加到一起。

为了懒惰,它看起来像这样

<button onclick=calculate()>Calculate Total</button>
<script type="text/javascript">
function calculate(); {
    var number1 = 2;
    var number2 = 3
var SumAll = number1 + number2;
//print the result in "Sum" paragraph, keep it to two decimals
document.getElementById("Sum").innerHTML = SumAll.toFixed(2);
    }
</script>

我想做的事情听起来很简单,但我无法理解。我想在警告框中打印结果。他的新手有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

如果你真的想要一个简单的警告框:

struct foo{
    int32 a,
    int16 b,
    int8 c,
    int32 d,
    int32 e
};

struct foo{  <-- aligned on a 32 bit address boundary by compiler
    int32 a, <-- +0 
    int16 b, <-- +4
    int8 c,  <-- +6
             <-- +7 (1 byte padding)
    int32 d, <-- +8
    int32 e  <-- +12  (not +16 as indicated in posted code)
};

在控制台中将其打印出来进行调试:

source 'https://rubygems.org'
ruby '2.1.5'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
  # Testing stuff
  gem 'rspec-rails'
  gem 'capybara'
  gem 'guard-rspec'
  gem 'wdm'
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

答案 1 :(得分:0)

你的按钮应该是这样的。

<button onclick="calculate()">Calculate Total</button>

你在错误的地方也有一个分号。

function calculate(); {

结果将被发送到DOM中的元素为“Sum”的元素。 要在警告框中显示var SumAll,请尝试:

function calculate() {
    var n1 = 2;
    var n2 = 3;
    var sum = n1 + n2;
    alert(sum.toFixed(2));
}