将var添加到新窗口Javascript

时间:2013-11-22 06:38:18

标签: javascript html

做一些HTML和javascript的东西,我似乎在这个地方停滞不前。 我所要做的就是执行一个简单的计算器并获取结果并将其显示在一个新窗口中。使用javascript打开新窗口但是无法获取结果并将其放入新的HTML窗口文本框中。

var totalarea是我需要添加到新窗口的值,是考虑将其放入HTMLstring + ='

\ n';某处

<html>
<head>
<title>Cross-Window HTML</title>
<script type="text/javascript">

function newPage() {
HTMLstring='<html>\n';
HTMLstring+='<head>\n';
HTMLstring+='<title>New Document</title>\n';
HTMLstring+='</head>\n';
HTMLstring+='<body>\n';
HTMLstring+='<p><input type="text" name="area" id="area"/> Is how much carpet you need                                   in feet.</p>\n';
HTMLstring+='</body>\n';
HTMLstring+='</html>';
newwindow=window.open();
newdocument=newwindow.document;
newdocument.write(HTMLstring);
newdocument.close();
}

function ComputeSquareFeet() {
var percent = .01;
var num1 = +document.getElementById("length").value;
var num2 = +document.getElementById("width").value;
var num3 = +document.getElementById("allowance").value;
var area = num1 * num2;
var totalarea = (num3 * percent * area) + area; 
}
function newfeetwin() {
ComputeSquareFeet()
newPage()
}
</script>
</head>
<body>

<h1>Carpet Calculator</h1>
<form name="userInputs">
<label for="lengths">Enter the length of your room in feet</label><br/>
<input type="Text" name="length" id="length" size="7"><br/>

<label for="widths">Enter the width of your room in feet</label><br/>
<input name = "width" id="width" type="text"size="7"><br/>

<label for="allowance">Typically an allowance is made for room irregularities and         unavoidable waste.<br/>
Enter the percent overage as an integer in the interval [0, 20]</label><br/>
<input name = "allowance" id="allowance" type = "text" /><br/>


<input type="button" name="CalcSqFeet" id="area" value="Calculate Square Feet" onClick="newfeetwin()" /><br/>

<br />
</body>
</html>

2 个答案:

答案 0 :(得分:0)

更改您的功能,如

function newPage(totalarea) {
HTMLstring='<html>\n';
HTMLstring+='<head>\n';
HTMLstring+='<title>New Document</title>\n';
HTMLstring+='</head>\n';
HTMLstring+='<body>\n';
HTMLstring+='<p><input type="text" name="area" id="area" value="'+totalarea+'"/> Is how much carpet you need                                   in feet.</p>\n';
HTMLstring+='</body>\n';
HTMLstring+='</html>';
newwindow=window.open();
newdocument=newwindow.document;
newdocument.write(HTMLstring);
newdocument.close();
}

function ComputeSquareFeet() {
var percent = .01;
var num1 = +document.getElementById("length").value;
var num2 = +document.getElementById("width").value;
var num3 = +document.getElementById("allowance").value;
var area = num1 * num2;
var totalarea = (num3 * percent * area) + area; 
return totalarea;
}
function newfeetwin() {
//ComputeSquareFeet()
newPage(ComputeSquareFeet())
}

答案 1 :(得分:0)

您应该从JavaScript函数返回值,然后将值传递给答案框。 按如下方式更改JavaScript代码。

<script type="text/javascript">

function newPage(dd) {
HTMLstring='<html>\n';
HTMLstring+='<head>\n';
HTMLstring+='<title>New Document</title>\n';
HTMLstring+='</head>\n';
HTMLstring+='<body>\n';
HTMLstring+='<p><input type="text" name="area" id="area" value="'+dd+'"/> Is how much carpet you need                                   in feet.</p>\n';
HTMLstring+='</body>\n';
HTMLstring+='</html>';
newwindow=window.open();
newdocument=newwindow.document;
newdocument.write(HTMLstring);
newdocument.close();
}

function ComputeSquareFeet() {
var percent = .01;
var num1 = +document.getElementById("length").value;
var num2 = +document.getElementById("width").value;
var num3 = +document.getElementById("allowance").value;
var area = num1 * num2;
var totalarea = (num3 * percent * area) + area; 
return totalarea;
}
function newfeetwin() {
    var dd = ComputeSquareFeet()
    newPage(dd);
}
</script> 

我觉得你有你想要的东西.. 欢呼声..