使用 JavaScript 的文本框自动结果出现故障
Glitch in automatic result of a textbox using JavaScript
我目前运行良好的代码符合预期。但是,结果存在小误差。我正在尝试根据产品价格和数量计算总价。(乘法)
在输出中,如果数量为 1,结果不会自动出现。我必须 select 其他值,然后再次 select 数量为 1,然后它显示正确的总价。发生这种情况是因为我选择数量的默认值为 1。我只是想知道如果我只想显示数字而不显示 "please select" 项目,如 that.Any 帮助表示赞赏
JavaScript:
<script type="text/javascript" language ="javascript">
function multiply() {
var x = document.getElementById("Quantityddl").value;
var y = document.getElementById("txtPrice").value;
if (x != null & y != null) {
document.getElementById("txtTotalPrice").value = parseInt(x) * parseInt(y);
}
}
</script>
ASP 下拉列表和文本框字段的代码:
<asp:DropDownList ID="Quantityddl" runat="server" onchange="multiply()">
<asp:ListItem Selected = "True" Value = "1">1</asp:ListItem>
<asp:ListItem Value = "2">2</asp:ListItem>
<asp:ListItem Value = "3">3</asp:ListItem>
<asp:ListItem Value = "4">4</asp:ListItem>
<asp:ListItem Value = "5">5</asp:ListItem>
<td>
<asp:TextBox ID="txtPrice" runat="server" onchange="multiply()"</asp:TextBox>
</td>
<td>
<asp:Button ID="btnNext" runat="server" Text="NEXT" onchange="multiply()" />
</td>
将文本框的onchange改为onkeyup,按钮的onchange改为onclick
如果您希望 select 也起作用,您必须添加一个 select 或使用 onload 来存储 selected 值 select。
原因是按钮的 onchange 没有意义,文本字段的 onchange 需要模糊才能触发
我还会添加基数并测试实际数字的字段
function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n);}
function multiply() {
var x = document.getElementById("Quantityddl").value;
var y = document.getElementById("txtPrice").value;
if (isNumeric(x) && isNumeric(y)) {
document.getElementById("txtTotalPrice").value = parseInt(x,10) * parseInt(y,10);
}
}
使用
<asp:DropDownList ID="Quantityddl" runat="server" onchange="multiply()">
<asp:ListItem Selected = "True" Value = "">Please select</asp:ListItem>
<asp:ListItem Value = "1">1</asp:ListItem>
<asp:ListItem Value = "2">2</asp:ListItem>
<asp:ListItem Value = "3">3</asp:ListItem>
<asp:ListItem Value = "4">4</asp:ListItem>
<asp:ListItem Value = "5">5</asp:ListItem>
<td>
<asp:TextBox ID="txtPrice" runat="server" onkeyup="multiply()"</asp:TextBox>
</td>
<td>
<asp:Button ID="btnNext" runat="server" Text="NEXT" onclick="multiply()" />
</td>
我目前运行良好的代码符合预期。但是,结果存在小误差。我正在尝试根据产品价格和数量计算总价。(乘法) 在输出中,如果数量为 1,结果不会自动出现。我必须 select 其他值,然后再次 select 数量为 1,然后它显示正确的总价。发生这种情况是因为我选择数量的默认值为 1。我只是想知道如果我只想显示数字而不显示 "please select" 项目,如 that.Any 帮助表示赞赏
JavaScript:
<script type="text/javascript" language ="javascript">
function multiply() {
var x = document.getElementById("Quantityddl").value;
var y = document.getElementById("txtPrice").value;
if (x != null & y != null) {
document.getElementById("txtTotalPrice").value = parseInt(x) * parseInt(y);
}
}
</script>
ASP 下拉列表和文本框字段的代码:
<asp:DropDownList ID="Quantityddl" runat="server" onchange="multiply()">
<asp:ListItem Selected = "True" Value = "1">1</asp:ListItem>
<asp:ListItem Value = "2">2</asp:ListItem>
<asp:ListItem Value = "3">3</asp:ListItem>
<asp:ListItem Value = "4">4</asp:ListItem>
<asp:ListItem Value = "5">5</asp:ListItem>
<td>
<asp:TextBox ID="txtPrice" runat="server" onchange="multiply()"</asp:TextBox>
</td>
<td>
<asp:Button ID="btnNext" runat="server" Text="NEXT" onchange="multiply()" />
</td>
将文本框的onchange改为onkeyup,按钮的onchange改为onclick
如果您希望 select 也起作用,您必须添加一个 select 或使用 onload 来存储 selected 值 select。
原因是按钮的 onchange 没有意义,文本字段的 onchange 需要模糊才能触发
我还会添加基数并测试实际数字的字段
function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n);}
function multiply() {
var x = document.getElementById("Quantityddl").value;
var y = document.getElementById("txtPrice").value;
if (isNumeric(x) && isNumeric(y)) {
document.getElementById("txtTotalPrice").value = parseInt(x,10) * parseInt(y,10);
}
}
使用
<asp:DropDownList ID="Quantityddl" runat="server" onchange="multiply()">
<asp:ListItem Selected = "True" Value = "">Please select</asp:ListItem>
<asp:ListItem Value = "1">1</asp:ListItem>
<asp:ListItem Value = "2">2</asp:ListItem>
<asp:ListItem Value = "3">3</asp:ListItem>
<asp:ListItem Value = "4">4</asp:ListItem>
<asp:ListItem Value = "5">5</asp:ListItem>
<td>
<asp:TextBox ID="txtPrice" runat="server" onkeyup="multiply()"</asp:TextBox>
</td>
<td>
<asp:Button ID="btnNext" runat="server" Text="NEXT" onclick="multiply()" />
</td>