使用javascript自动填充表单字段

时间:2015-02-23 06:09:12

标签: javascript html5 forms

很抱歉这样一个noob问题 - 在浏览了很多帖子之后我没有找到解决方案,我担心我甚至不知道要问什么问题 - 我正试图自动填充'sunits'输入字段到'bunits'输入的值,除非改变。我几乎没有为一些朋友拼凑这些,并为可怜的风格和其他一切道歉。以下代码也是http://lootsim.com的主页:

<div id="buyformdiv" class="fluid ">
<span id="pickingupspan" class="actionspan">Buying:</span>

<p># units to be bought:
<input id="bunits" type="number" step="1" onchange="update()"></p>
<p>
Cost per unit :
<input id="cunits" type="number" step="0.01" onchange="update()"></p>
<p>
Overhead :
<input id="cxbship" type="number" step="0.01" onchange="update()"></p>


</div>
<div id="sellformdiv" class="fluid ">
<span id="gettingridspan" class="actionspan">Selling:</span>
<p># units to be sold:
<input id="sunits" type="number" step="1" onchange="update()"></p>
<p>
selling for ($ each) :
<input id="sprice" type="number" step="0.01" onchange="update()"></p>
<p>
Frequency :
<select id="sfreq" onchange="update()">
<option value="1">Once</option>
<option value="7">Week</option>
<option value="31">Month (31 days)</option>
<option value="365">Year</option>
</select>
</p>

</div>

的Javascript“

var bunits, cunits, cxb, cxbship, btotal, sunits, sfreq, sprice, net, profit;

function update() {

var bunits = document.getElementById('bunits').value;


var cunits = document.getElementById('cunits').value;
var cunits = parseFloat(cunits).toFixed(2);

var cxbship = document.getElementById('cxbship').value;
var cxbship = parseFloat(cxbship).toFixed(2);

var cxb = bunits * cunits;
var cxb = parseFloat(cxb).toFixed(2);

var btotal = parseFloat(cxb) + parseFloat(cxbship);
var btotal = parseFloat(btotal).toFixed(2);

var sunits = document.getElementById('sunits').value;


var sprice = document.getElementById('sprice').value;
var sprice = parseFloat(sprice).toFixed(2);

var sfreq = document.getElementById('sfreq').value;

var sunitxp = sunits * sprice;
var sunitxp = parseFloat(sunitxp).toFixed(2);

var salesxdays = sunitxp * sfreq;
var salesxdays = parseFloat(salesxdays).toFixed(2);



//populate numbers in results window
if (bunits > 0) {
document.getElementById('bunitsspan').innerHTML = "Buying " + bunits + " units";
document.getElementById('sunits').value = bunits;
}
if (cunits > 0) {
document.getElementById('costspan').innerHTML = "at $" + cunits + " per unit";
}
if (cxb > 0) {
document.getElementById('cxbspan').innerHTML = "Subtotal: $" + cxb;
}
if (cxbship > 0) {
document.getElementById('cxbshipspan').innerHTML = "Overhead: " + cxbship;
}
if (btotal > 0) {
document.getElementById('subtotalspan').innerHTML = "<b>Total: $" + btotal + "</b>";
}

//populate income in results span
if (sunits > 0) {
document.getElementById('sunitsspan').innerHTML = "Selling " + sunits + " units";
}

if (sprice > 0) {
document.getElementById('sellcostspan').innerHTML = "for $" + sprice + " each";
}
if (sunitxp > 0) {
document.getElementById('sunitxpspan').innerHTML = "Subtotal: $" + sunitxp;
}
if (sfreq > 0 ) {
            if ((sfreq == 1) && (sprice > 0)){
document.getElementById('sellfreqspan').innerHTML = "all units";
} else if (sfreq == 7) {
    document.getElementById('sellfreqspan').innerHTML = "every day for a week";
} else if (sfreq == 31) {
    document.getElementById('sellfreqspan').innerHTML = "every day for a month";
} else if (sfreq == 365) {
       document.getElementById('sellfreqspan').innerHTML = "every day for a year";
}

}

if (salesxdays > 0) {
document.getElementById('salesxdays').innerHTML = "Total: $" + salesxdays;
}
}

0 个答案:

没有答案