我正在尝试制作一个矢量计算器来练习我的jquery技巧,我有一个问题。我有一个.option div,我有力量和角度的场。如果我单击#calc_v按钮,则会插入另一个字段。
<div class="option">
<h2>Vectors</h2>
<a class="close">Close X</a><br/>
<div id="move">1.<input type="number" step="0.01" id="N1"/> Newtons(or meters) à <input type="number" step="0.01" id="A1"/> Degrees
<br/></div>
<button id="calc_v">Calculer</button>
<a id="add_move">+ VECTOR</a>
<div id="log_v"></div>
这是我的jquery代码:
<script>
//vectors
var vector = 1;
$("#add_move").click(function () {
vector++;
$("#move").append(vector+".<input type='number' step='0.01' id='N"+vector+"'/> Newtons(ou mètres) à <input type='number' step='0.01' id='A"+vector+"'/> Degrées<br/>");
});
$("#calc_v").click(function(){
var x_v = 0;
var y_v = 0;
var ite = 0;
for(x=1; x <= vector; x++){
//for each added vector give it a var name
ite++;
var f = "#N"+ite;
var a = "#A"+ite;
//then, find its value
var force = $(f).val();
var f_val = parseFloat(force);
var angle = $(a).val();
var a_val = parseFloat(angle);
//analyse it
var cons_x = ((f_val)*(Math.cos(a_val))) * (180 / Math.PI);
var cons_y = ((f_val)*(Math.sin(a_val))) * (180 / Math.PI);
//add it to each x,y coords
x_v += cons_x;
y_v += cons_y;
}
$("#log_v").html("Coords: ("+ x_v + ","+ y_v + ") |f"+force +"|a"+ angle +"|i"+ ite+"|v"+vector);
});