0

I need some help putting together a line of javascript. I Am looking to add the values of two different fields and display them in a text area. The fields are "AP" and "MP" and the text are is "TotalCost".

I am not sure of how to make his happen at all. Please help!

4

2 回答 2

0

HTML:

<input name="AP" />
<input name="MP" />
<textarea name="TotalCost" rows="4" cols="20"></textarea>
<button>Add</button>

jQuery:

$(document).ready(function(){
    $("button").click(function(){
       $("textarea[name='TotalCost']").val($("input[name='AP']").val()+$("input[name='MP']").val());
    });
});

工作小提琴

于 2013-02-14T01:38:04.013 回答
0

所以像:

<input id="AP" />
<input id="MP" />
<input id="TotalCost" />
<button onclick="add()">Add</button>

<script>
function add()
{
   var ap = +document.getElementById('AP').value; // Value of AP (plus will cast the value to a number)
   var mp = +document.getElementById('MP').value; // Value of MP
   document.getElementById('TotalCost').value = ap + mp; // Set TotalCost to ap + mp
}
</script>

如果你想使用 jQuery,你可以这样做:

<button id="btnAdd">Add</button>

<script>    
   $('#btnAdd').click(function ()
   { 
       var ap = +$('#AP').val();
       var mp = +$('#MP').val();
       $('#TotalCost').val(ap + mp);
   });
</script>
于 2013-02-14T01:25:17.237 回答