我正在研究一个简单的脚本,并且除了如何为输出设置默认值之外,几乎已经弄明白了。如何让“Total”显示默认值0,就像页面加载时一样?谢谢!
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> demo</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.2.js'></script>
<style type='text/css'>
#multi
{
margin-left:25px;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
$('input').on('keyup click',function(event){ // run anytime the value changes
var firstValue = parseInt($('#quantity').val()) || 0; // get value of field
var total = firstValue * 5; // multiply them together
$('#multi').html(total); // output it
});
});
</script>
</head>
<body>
<label for="quantity">Quantity</label>
<input type="number" id="quantity" min="0" max="250" size="3" value="0"/>
<div>Total<span id="multi"></span><br></div>
</body>
</html>
答案 0 :(得分:1)
把它扔进HTML!
<div>Total<span id="multi">0</span><br></div>
答案 1 :(得分:0)
我不确定你在问什么,但你不能只做$('#multi').html('Total:' + total);
吗?