javascript值获取
我想用JavaScript创建一个股票计算系统,现在我已经创建了它,有一些问题,问题是我已经创建了3行1.购买,2.出售,3.库存。现在我已经设置,当我给出采购值和销售数量时,它将减去并在库存文本框中打印值,然后是下一列。库存值也将打印在采购文本框中。问题是,当采购项目变为零时,表示0,那么我可以做些什么来添加一些数量并重新开始此过程。 编码是javascript值获取,javascript,Javascript,我想用JavaScript创建一个股票计算系统,现在我已经创建了它,有一些问题,问题是我已经创建了3行1.购买,2.出售,3.库存。现在我已经设置,当我给出采购值和销售数量时,它将减去并在库存文本框中打印值,然后是下一列。库存值也将打印在采购文本框中。问题是,当采购项目变为零时,表示0,那么我可以做些什么来添加一些数量并重新开始此过程。 编码是 <html> <head> <title>Stock Information</title> <l
<html>
<head>
<title>Stock Information</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script type="text/javascript">
//------------------------------------Date---------------------------------------------
var today = new Date(); //
var dd = today.getDate(); //
var mm = today.getMonth()+1; //January is 0! //
var yyyy = today.getFullYear(); //
today = dd+'/'+mm+'/'+yyyy; //
document.write(today); //
//-------------------------------------------------------------------------------------
//----------------------------------------------------
function program(a,b,c,d,e,f)
{
var total1=a-b;
document.stock.t3.value=total1;
var jump1=document.stock.t3.value;
document.stock.t4.value=jump1;
var total2= c-d;
document.stock.t6.value=total2;
var jump2=document.stock.t6.value;
document.stock.t7.value=jump2;
var total3= e-f;
document.stock.t9.value=total3;
}
</script>
</head>
<body>
<center>
<h1 class="table">Stock Information</h1>
</center>
<table width="800" align="center" class="table1">
<tr>
<th width="260"class="table">Purchase</th>
<th width="260"class="table">Sold</th>
<th width="428"class="table">Stock</th>
</tr>
<form name="stock">
<tr align="center">
<td><input id="t1" type="text" style="width:260px;" class="input1"/></td>
<td><input id="t2" type="text" style="width:260px;" class="input1"/></td>
<td><input id="t3" type="text" style="width:260px;" class="input1" onClick="program(t1.value,t2.value)" readonly/></td>
</tr>
<tr align="center">
<td><input id="t4" type="text" style="width:260px;" class="input1" onClick="program(t1.value,t2.value,t4.value,t5.value)"/></td>
<td><input id="t5" type="text" style="width:260px;" class="input1"/></td>
<td><input id="t6" type="text" style="width:260px;" class="input1" onClick="program(t1.value,t2.value,t4.value,t5.value)" readonly/></td>
</tr>
<tr align="center">
<td><input id="t7" type="text" style="width:260px;" class="input1" onClick="program(value1.value,value2.value,t4.value,t5.value)"/></td>
<td><input id="t8" type="text" style="width:260px;" class="input1"/></td>
<td><input id="t9" type="text" style="width:260px;" class="input1" onClick="program(value1.value,value2.value,t4.value,t5.value,t7.value,t8.value)"readonly/></td>
</tr>
</form>
</table>
</body>
</html>
股票信息
//------------------------------------日期---------------------------------------------
var today=新日期()//
var dd=today.getDate()//
var mm=today.getMonth()+1//一月是零//
var yyyy=today.getFullYear()//
今天=dd+'/'+mm+'/'+yyyy;//
文件。写(今天)//
//-------------------------------------------------------------------------------------
//----------------------------------------------------
功能程序(a、b、c、d、e、f)
{
var total1=a-b;
document.stock.t3.value=total1;
var jump1=document.stock.t3.value;
document.stock.t4.value=1;
var total2=c-d;
document.stock.t6.value=total2;
var jump2=document.stock.t6.value;
document.stock.t7.value=jump2;
var total3=e-f;
document.stock.t9.value=total3;
}
股票信息
购买
出售
股票
我建议您保持JavaScript中的逻辑并保持HTML的干净!:)
您可以使用onChange
<input onChange="program(this)" />
我不知道它到底是如何工作的,但我会尝试重复同样的逻辑,你做了什么
我稍微更改了HTML结构:
<form name="stock">
<table width="800" align="center" class="table1">
<tr>
<th width="260"class="table">Purchase</th>
<th width="260"class="table">Sold</th>
<th width="428"class="table">Stock</th>
</tr>
<tr align="center">
<td><input id="t1" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t2" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t3" type="text" style="width:260px;" class="input1" readonly /></td>
</tr>
<tr align="center">
<td><input id="t4" type="text" style="width:260px;" class="input1" readonly /></td>
<td><input id="t5" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t6" type="text" style="width:260px;" class="input1" readonly /></td>
</tr>
<tr align="center">
<td><input id="t7" type="text" style="width:260px;" class="input1" readonly /></td>
<td><input id="t8" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t9" type="text" style="width:260px;" class="input1" readonly /></td>
</tr>
</table>
</form>
您还可以在JSBin中查看此演示:
我希望我做对了
更新
好的,我做了一些改进。但是,我没有检查结果是否为0
,而是首先允许写。当结果被复制到下一行的第一个字段时,该函数现在只是一个助手,如果该字段不是空的,则不会覆盖该值。我想应该是这样的
稍微更改的HTML:
<form name="stock">
<table width="800" align="center" class="table1">
<tr>
<th width="260"class="table">Purchase</th>
<th width="260"class="table">Sold</th>
<th width="428"class="table">Stock</th>
</tr>
<tr align="center">
<td><input id="t1" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t2" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t3" type="text" style="width:260px;" class="input1" readonly="readonly" /></td>
</tr>
<tr align="center">
<td><input id="t4" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t5" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t6" type="text" style="width:260px;" class="input1" readonly="readonly" /></td>
</tr>
<tr align="center">
<td><input id="t7" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t8" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t9" type="text" style="width:260px;" class="input1" readonly="readonly" /></td>
</tr>
</table>
</form>
这个演示可以在这里进行测试:
请把这个答案投票给我并给我一些分数!:) 请更谨慎地解释!先生,在这个编码中有三行三列,我有三列,分别是1.购买,2.出售,3.库存。然后我在这些文本框上应用了javascript,现在我希望当我在购买文本框和售出文本框中给出值时,会在下一个文本框中给出结果,即股票。然后,在下一列中,无论uper stock文本框中的值是多少,都将在purchase文本框中打印。现在主要的问题是当购买值变为零时,那么我如何在购买文本框中赋予更多的值来重新开始这个过程呢。然后有权输入购买价值,然后开始上一个过程。@Mufasil.Muhammad.Iqbal我仍然不知道你想要做什么。因此,
t4
和t7
是只读的
,但是当它们变成0
时,它们不再是只读的
?先生,是的,当t4值变为零时,t7会自动地自动删除,就像t1@Mufasil.Muhammad.Iqbal我确实更新了答案。:)非常感谢你,塔伊
function program(el) {
var fields = document.stock;
// Clear the calculation fields
fields.t3.value = '';
fields.t6.value = '';
fields.t9.value = '';
fields.t4.value = '';
fields.t7.value = '';
// Do nothing, if any of the required fields are empty
if (fields.t1.value === '' || fields.t2.value === '') return;
// Calculate result
fields.t3.value = fields.t1.value - fields.t2.value;
// Copy the result
fields.t4.value = fields.t3.value;
// Do nothing, if the required fields are empty
if (fields.t5.value === '') return;
// Calculate result
fields.t6.value = fields.t4.value - fields.t5.value;
// Copy the result
fields.t7.value = fields.t6.value;
// Do nothing, if the required fields are empty
if (fields.t8.value === '') return;
// Calculate result
fields.t9.value = fields.t7.value - fields.t8.value;
}
<form name="stock">
<table width="800" align="center" class="table1">
<tr>
<th width="260"class="table">Purchase</th>
<th width="260"class="table">Sold</th>
<th width="428"class="table">Stock</th>
</tr>
<tr align="center">
<td><input id="t1" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t2" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t3" type="text" style="width:260px;" class="input1" readonly="readonly" /></td>
</tr>
<tr align="center">
<td><input id="t4" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t5" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t6" type="text" style="width:260px;" class="input1" readonly="readonly" /></td>
</tr>
<tr align="center">
<td><input id="t7" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t8" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
<td><input id="t9" type="text" style="width:260px;" class="input1" readonly="readonly" /></td>
</tr>
</table>
</form>
function program(el) {
// Get the row (the <tr> element)
var row = el.parentNode.parentNode;
// Get all the <input> elements in row
var inputs = row.getElementsByTagName('input');
// Return, if required fields are empty
if (inputs[0].value === '' || inputs[1].value === '') {
inputs[2].value = '';
return;
}
// Calculate the result
inputs[2].value = inputs[0].value - inputs[1].value;
// Find the next row (<tr> element)
var nextRow = row.nextSibling.nextSibling;
while (nextRow !== null && nextRow.tagName !== 'TR') {
nextRow = nextRow.nextSibling;
}
// Return, if the next row doesn't exist
if (nextRow === null || nextRow.tagName !== 'TR') return;
// Get all <input> fields in row
var nextRowInputs = nextRow.getElementsByTagName('input');
if (nextRowInputs[0].value === '') {
// Copy the previous row result into next rows first input,
// only if it's not filled already
nextRowInputs[0].value = inputs[2].value;
}
}