日期:2014-05-16  浏览次数:20374 次

求一JS功能,第一个对的给100分!
有一表单如下:

成本价:_____
毛利率:_____
销售价:_____

***************************************************************

上面的HTML代码如下:

成本价: <input   name= "price1 "   type= "text "   size= "10 ">   <br>
毛利率: <input   name= "mao_li "   type= "text "   size= "10 ">   <br>
销售价: <input   name= "price2 "   type= "text "   size= "10 ">   <br>

***************************************************************

现在我要实现的功能如下:
1、当用户输入成本价和毛利率后,鼠标移到销售价文本框时,用JS功能自动计算并显示出销售价。
2、当用户输入成本价和销售价后,鼠标移到毛利率文本框时,用JS功能自动计算并显示出毛利率。

计算公式是:
销售价=[成本价/(1-毛利率)]*1.17

**************************************************************

请知道的XDJM帮忙解决此问题啊,急!!!
分不够可以再加。


------解决方案--------------------
try
------------------------------------
<script type= "text/javascript ">
function calc(){
var price = document.getElementsByName( "price1 ")[0];
var maoLi = document.getElementsByName( "mao_li ")[0];
var sprice = document.getElementsByName( "price2 ")[0];
if(price.value!= " " && maoLi.value!= " "){
sprice.value=1.17 * price.value/(1-maoLi.value);
}
if(price.value!= " " && sprice.value!= " "){
maoLi.value=1- price.value/sprice.value*1.17;
}
}
</script>

成本价: <input name= "price1 " type= "text " size= "10 " onchange= "calc(); "> <br>
毛利率: <input name= "mao_li " type= "text " size= "10 " onchange= "calc(); "> <br>
销售价: <input name= "price2 " type= "text " size= "10 " onchange= "calc(); "> <br>
------解决方案--------------------
呵呵~~~
function doMath(n)
{
var p2 = mathe.price2.value;
var p1 = mathe.price1.value;
var m1 = mathe.mao_li.value;

if(n == 'price2 ' && p1 != ' ' && m1 != ' ')
{

mathe.price2.value = (p1/(1-m1))*1.17;
return true;
}
else if(n == 'mao_li ' && p1!= ' ' && p2 != ' ')
{
mathe.mao_li.value = 1-p1/(p2/1.17);
return true;
}
else return false;
}

<form name= "mathe " method= "POST ">
成本价: <input name= "price1 " type= "text " size= "10 "> <br>
毛利率: <input name= "mao_li " type= "text " size= "10 " onClick= "doMath(this.name) "> <br>
销售价: <input name= "price2 " type= "text " size= "10 " onClick= "doMath(this.name) "> <br>
<form>