js厘米与英寸尺码转换

时间:2023-03-09 02:27:17
js厘米与英寸尺码转换
 <style type="text/css">
#txt_cm1, #txt_inch1, #txt_inch2, #txt_cm2 {
width: 63px;
height: 26px;
margin: 0 5px;
border: solid 1px #ccc;
}
#txt_inch1, #txt_cm2 {
background: #e7e7e7;
text-align: right;
padding-right: 5px;
width: 58px;
}
.converterCalculation{
overflow: hidden;
border: 1px solid #e7e7e7;
padding: 15px;
}
.converterCalculation .calculation{
color: #000;
height: 33px;
font-weight: bold;
width: 365px;
padding-right: 0;
}
.converterCalculation div.calculation input.last {
height: 26px;
line-height: 26px;
color: #fff;
background: #999;
padding: 0 5px;
margin-left: 5px;
}
</style>
<div class="converterCalculation">
<div class="calculation">Converter:
<input type="text" id="txt_cm1" onkeypress="keyPress(this)" onkeyup="keyUp(this)" onblur="onBlur(this)" />cm&nbsp;&nbsp;
<input type="text" id="txt_inch1" disabled="disabled" value="0.00" />inch
<input type="button" class="last" value="Calculation" onclick="Calculation(1)" />
</div>
<div class="calculation">Converter:
<input type="text" id="txt_inch2" onkeypress="keyPress(this)" onkeyup="keyUp(this)" onblur="onBlur(this)" />inch
<input type="text" id="txt_cm2" disabled="disabled" value="0.00" />cm&nbsp;&nbsp;
<input type="button" class="last" value="Calculation" onclick="Calculation(2)" />
</div>
</div>
<script type="text/javascript"> function keyPress(that){
that.value.match(/^[\+\-]?\d*?\.?\d*?$/)?that.t_value=that.value:that.value=that.t_value;
that.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/)&&(that.o_value=that.value)
}
function keyUp(that){
that.value.match(/^[\+\-]?\d*?\.?\d*?$/)?that.t_value = that.value:that.value = that.t_value;
that.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/) && (that.o_value=that.value)
}
function onBlur(that){
that.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/)?(
that.value.match(/^\.\d+$/)&&
(that.value=0+that.value),that.value.match(/^\.$/)&&
(that.value=0),that.o_value=that.value
):that.value = that.o_value
}
function Calculation(type){
var cm1,inch1,inch2,cm2;
type==1 && (
cm1=eval($("#txt_cm1").val()),
cm1==undefined&&(cm1=0),inch1=(cm1/2.54).toFixed(2),
$("#txt_inch1").val(inch1)
);
type==2 && (
inch2=eval($("#txt_inch2").val()),
inch2==undefined&&(inch2=0),cm2=(inch2*2.54).toFixed(2),
$("#txt_cm2").val(cm2)
)
}
</script>