【问题标题】:How to set default output when using jQuery .html()?使用 jQuery .html() 时如何设置默认输出?
【发布时间】:2013-07-15 19:00:10
【问题描述】:

我正在编写一个简单的脚本,除了如何为输出设置默认值之外,几乎已经弄清楚了。如何让“总计”显示默认值 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>

【问题讨论】:

    标签: jquery html default output


    【解决方案1】:

    把它扔到 HTML 中!

    <div>Total<span id="multi">0</span><br></div>
    

    【讨论】:

    • 编辑:我的错,非常感谢!太简单了,我现在觉得很傻。大声笑
    【解决方案2】:

    我不确定你在问什么,但你不能只做$('#multi').html('Total:' + total);吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2019-11-21
      相关资源
      最近更新 更多