簡體   English   中英

jQuery val()設置表單值時引發運行時錯誤

[英]JQuery val() throws runtime error when setting form value

我是Javascript / JQuery的新手,如果我錯過了一些簡單的內容,我們深表歉意。 我正在嘗試創建一個按鈕,以根據數量和單價計算總數,並用結果填充總數字段。 這是使用VS2010在ASP.NET MVC3中完成的。

我的代碼似乎可以將SpoilageQuantity和SpoilageUnitPrice的值確定為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)找不到元素。 如果我將腳本包含在標簽中,則一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM