根据变量计算更改HTML段落

时间:2015-06-26 17:51:45

标签: jquery html

我想将两个变量相乘并将结果输出到段落中。

这是我的剧本

function product() {
var first = $('input[name=amount]').val();
var second = $('#ipayoutdyn').val();
var productof = first*second;
$("#iwindyn").html(productof);
}

HTML

<input id='inpamount' type="text" name="amount" value="2.00" onkeyup="pad();validatemin();product()">

<p id = "iwindyn">3.84</p>

我试过这个,但它似乎不起作用。有谁能提出问题?感谢

使用parseFloat()并为HTML元素指定文本值解决了问题。

function product() {
var first = $('input[name=amount]').val();
var second = parseFloat($('#sidetextdyn').text());
var productof = first*second;
$("#iwindyn").html(productof);
}

1 个答案:

答案 0 :(得分:1)

您的amount名称为var first = $('input[name="amount"]').val(); 的选择器需要引号:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="amount"/>
<input id="ipayoutdyn"/>
<input id='inpamount' type="text" name="amount" value="2.00" onkeyup="product()"/>
<div id="iwindyn"></div>

<script>
    function product() {
        var first = $('input[name="amount"]').val();
        var second = $('#ipayoutdyn').val();
        var productof = first*second;
        $("#iwindyn").html(productof);
    }    
</script>

product()

现在当您输入第3个文本框时,它会触发#iwindyn函数,结果显示为{ "Version": "2012-10-17", "Statement": [ { "Sid": "Stmt1385026304010", "Effect": "Allow", "Action": [ "ec2:RunInstances" ], "Condition": { "StringEquals": { "ec2:InstanceType": "t2.medium" } }, "Resource": [ "arn:aws:ec2:us-east-1:*:instance/*", "arn:aws:ec2:us-east-1:*:image/ami-81c603ea", "arn:aws:ec2:us-east-1:*:key-pair/CodeDeploy", "arn:aws:ec2:us-east-1:*:security-group/sg-5ace243d", "arn:aws:ec2:us-east-1:*:volume/*" ] } ] }

希望这有帮助!