JQuery val()在设置表单值时引发运行时错误
JQuery val() throws runtime error when setting form value
我是Javascript/JQuery的新手,如果我错过了一些简单的东西,我很抱歉。我正在试着做一个按钮,从一个数量计算总数;单价,并用结果填充合计字段。这是在ASP.NET MVC3中使用VS2010完成的。
我的代码将显示"SpoilegeQuantity"answers"SpoileageUnitPrice OK"的值,但无法设置"SpoilageTotalPrice"的值。错误表明我正在尝试为函数结果赋值,但我的理解是,val()可以用于获取和设置表单字段值。更正或建议?
VS2010视图:
<div class="editor-label">
@Html.LabelFor(model => model.SpoilageQuantity)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SpoilageQuantity)
@Html.ValidationMessageFor(model => model.SpoilageQuantity)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.SpoilageUnitPrice)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SpoilageUnitPrice)
@Html.ValidationMessageFor(model => model.SpoilageUnitPrice)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.SpoilageTotalPrice)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SpoilageTotalPrice)
@Html.ValidationMessageFor(model => model.SpoilageTotalPrice)
<input type="button" value="Calculate" onclick="calculateTotal()" />
</div>
JS文件:
function calculateTotal() {
// calculate total spoilage price from unit price and quantity
var totalprice = $('#SpoilageQuantity').val() * $('#SpoilageUnitPrice').val();
totalprice = totalprice.toFixed(2);
$('#SpoilageTotalPrice').val(totalprice);
}
渲染HTML:
<div class="editor-label">
<label for="SpoilageQuantity">Item Quantity</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-number="The field Item Quantity must be a number." data-val-required="Quantity of item lost is required." id="SpoilageQuantity" name="SpoilageQuantity" type="text" value="0.00" />
<span class="field-validation-valid" data-valmsg-for="SpoilageQuantity" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="SpoilageUnitPrice">Unit Price</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-number="The field Unit Price must be a number." data-val-required="Unit price is required." id="SpoilageUnitPrice" name="SpoilageUnitPrice" type="text" value="0.00" />
<span class="field-validation-valid" data-valmsg-for="SpoilageUnitPrice" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="SpoilageTotalPrice">Total Price</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-number="The field Total Price must be a number." data-val-required="The Total Price field is required." id="SpoilageTotalPrice" name="SpoilageTotalPrice" type="text" value="0.00" />
<span class="field-validation-valid" data-valmsg-for="SpoilageTotalPrice" data-valmsg-replace="true"></span>
<input type="button" value="Calculate" onclick="calculateTotal()" />
</div>
错误消息:
Microsoft JScript runtime error: Cannot assign to a function result
编辑:整个js文件:
/* Copyright 2012 Tom Barrett
This file is part of PFC Logs.
PFC Logs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PFC Logs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PFC Logs. If not, see <http://www.gnu.org/licenses/>.
*/
function autoFocus() {
// Auto-focus on the first text field upon page load.
$("input[type=text]").first().focus();
}
function calculateTotal() {
// calculate total from quantity & unit price
var totalprice = $('#SpoilageQuantity').val() * $('#SpoilageUnitPrice').val();
totalprice = totalprice.toFixed(2);
$("#SpoilageTotalPrice").val(totalprice);
}
我不知道技术细节。。。但我也遇到过这种情况。如果我的函数在一个单独的js文件中,我在标记jquery(或Jscript)中链接到该文件,则无法找到元素。如果我将脚本包含在标记中,那么一切都会很好。
相关文章:
- JavaScript错误:Microsoft JScript运行时错误:应为对象
- 如何在submit to sightly时将表单输入值作为参数传递给WCMUsePojo
- 返回时清洁表单
- 如何使用jquery在单击按钮时显示表单
- 如何在使用AngularJS更改下拉值时清除表单中的数据
- 使用Backbone在模型更改时渲染表单会导致表单UI错误
- 如何在运行总计计算器时返回表单输入并更新 JavaScript 变量
- 在运行时更改表单标签文本
- JQuery val()在设置表单值时引发运行时错误
- 如何避免在出现错误时提交表单
- JavaScript错误表单验证:提交时清除所有输入字段
- 尝试在 ASP.Net 中使用 jquery 数据表时出现 Jscript 运行时错误
- Microsoft JScript运行时错误:表单中需要对象
- 如何在使用本地存储数据生成字段时删除表单验证错误
- 允许用户在运行时向表单添加字段
- 当表单要求没有满足时,表单onClick运行
- Jquery通过ajax调用外部PHP文件来验证表单,该文件检查所有字段,并在出现错误消息时阻止表单提交
- ASP.NET MVC -使用javascript在运行时添加表单元素
- 如何仅在提交时验证表单,并以角度显示警报表单中的错误消息
- 如何仅在有人成功提交表单时运行 PHP 邮件表单脚本