[英]How to check if a number is greater than another number in some quantity in javaScript?
[英]How to check if value is greater than another using javascript
我正在使用javascript檢查用戶輸入的值是否大於那里的值。 但是,當我單擊更新時,它僅重定向到另一個頁面,而沒有給出錯誤消息。
使用Javascript
<script type="text/javascript">
$(function onUpdateClick () {
var initVal = $('#quantity').val();
$('#quantity').change(function () {
if ($('#quantityI').val() > initVal)
console.log('Quantity issued should be less than quantity requested');
else {
location.href = '@Url.Action("Index","Issue")';
}
});
視圖
<div class="form-group">
@Html.LabelFor(model => model.item.quantity, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.item.quantity, new { htmlAttributes = new { @class = "form-control" , @readonly="readonly", @id=quantity} })
@Html.ValidationMessageFor(model => model.item.quantity, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.item.quantityI, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.item.quantityI, new { htmlAttributes = new { @class = "form-control", @id=quantityI } })
@Html.ValidationMessageFor(model => model.item.quantityI, "", new { @class = "text-danger" })
</div>
型號[必填]
[Range(0.5,double.MaxValue, ErrorMessage = "You must enter an amount greater than Zero")]
[Display(Name ="Quantity Requested")]
public double quantity { get; set; }
[Required]
[Range(0.5, double.MaxValue, ErrorMessage = "You must enter an amount greater than Zero")]
[Display(Name = "Quantity Issued")]
public double quantityI { get; set; }
我發現您的代碼有問題,獲取值時需要使用parseInt()
函數。
以下正在工作
<input type="Text" id="quantity" />
<input type="Text" id="quantityI" />
$(function()
{
$('#quantity').change(function ()
{
var quantityI = parseInt($('#quantityI').val());
var quantity = parseInt($('#quantity').val());
alert(quantityI);alert(quantity);
if( quantityI > quantity)
{
alert('Quantity issued should be less than quantity requested');
}
else {
alert('Else called');
}
});
});
工作演示: jsfiddle
您的描述缺少一些步驟。 無論如何,我會嘗試提出一些觀點,希望對您有所幫助。 控件的初始值是一個空字符串(據我所知),因此比較它們將得到兩個空字符串的字符串比較。 請注意,在某些版本的FireFox中,最終控件的值是在失去焦點之后設置的,因此有時您獲得的值是onchage事件中的舊值...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.