1. html内
<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="utf-8">
<title></title>
<script src="./loan5.js"></script>
</head>
<body>
<form name="F1">
<li><input class="tb_fmt1" name="h_feet" type="text" />feet</li>
<li><input class="tb_fmt1" name="h_inch" type="text" />inch </li>
<li><input type="button" value="submit" onclick="calc()"></li>
<li><input class="tb_fmt2" name="result" type="text" /> result </li>
</form>
<script type="text/javascript" >
/**************************
* カンマ編集を行うFunction
**************************/
function toComma(obj){
if((obj.value).trim().length != 0 && !isNaN(obj.value)){
obj.value = Number(obj.value).toLocaleString();
}
}
/**************************
* カンマ編集を解除するFunction
**************************/
function offComma(obj){
var reg = new RegExp(",", "g");
var chgVal = obj.value.replace(reg, "");
if(!isNaN(chgVal)){
obj.value = chgVal; //値セット
obj.select(); //全選択
}
}
window.onload = function(){
var elements = document.getElementsByClassName("tb_fmt1");
for(var i=0;i<elements.length;i++){
elements[i].onfocus = function(){offComma(this);}
elements[i].onblur = function(){toComma(this);}
}
}
function calc(){
// カンマ編集を解除するFunction
var a = parseFloat(document.F1.h_feet.value.replace(/,/g,''));
var b = parseFloat(document.F1.h_inch.value.replace(/,/g,''));
var result = a + b;
function thousands_separators(result) {
var num_parts = result.toString().split(".");
num_parts[0] = num_parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return num_parts.join(".");
}
document.F1.result.value =thousands_separators(result);
}
</script>
</body>
</html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title></title>
<script src="./loan5.js"></script>
</head>
<body>
<form name="F1">
<li><input class="tb_fmt1" name="h_feet" type="text" />feet</li>
<li><input class="tb_fmt1" name="h_inch" type="text" />inch </li>
<li><input type="button" value="submit" onclick="calc()"></li>
<li><input class="tb_fmt2" name="result" type="text" /> result </li>
</form>
<script type="text/javascript" >
/**************************
* カンマ編集を行うFunction
**************************/
function toComma(obj){
if((obj.value).trim().length != 0 && !isNaN(obj.value)){
obj.value = Number(obj.value).toLocaleString();
}
}
/**************************
* カンマ編集を解除するFunction
**************************/
function offComma(obj){
var reg = new RegExp(",", "g");
var chgVal = obj.value.replace(reg, "");
if(!isNaN(chgVal)){
obj.value = chgVal; //値セット
obj.select(); //全選択
}
}
window.onload = function(){
var elements = document.getElementsByClassName("tb_fmt1");
for(var i=0;i<elements.length;i++){
elements[i].onfocus = function(){offComma(this);}
elements[i].onblur = function(){toComma(this);}
}
}
function calc(){
// カンマ編集を解除するFunction
var a = parseFloat(document.F1.h_feet.value.replace(/,/g,''));
var b = parseFloat(document.F1.h_inch.value.replace(/,/g,''));
var result = a + b;
function thousands_separators(result) {
var num_parts = result.toString().split(".");
num_parts[0] = num_parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return num_parts.join(".");
}
document.F1.result.value =thousands_separators(result);
}
</script>
</body>
</html>
2.js.ファイル内
function calc(){
var num1 = parseFloat(document.F1.h_feet.value);
var num2 = parseFloat(document.F1.h_inch.value);
var num3 = num1+num2;
document.F1.result.value =num3;
}
var num1 = parseFloat(document.F1.h_feet.value);
var num2 = parseFloat(document.F1.h_inch.value);
var num3 = num1+num2;
document.F1.result.value =num3;
}
3.結果
0 件のコメント:
コメントを投稿