javascript - 如何使用javascript检查值是否大于另一个值

标签 javascript jquery asp.net-mvc

我正在使用 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

关于javascript - 如何使用javascript检查值是否大于另一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47655112/

相关文章:

javascript - 未找到。在此服务器上找不到请求的 URL/print_dokument.php

javascript - Jquery发送post表单ajax不起作用

c# - 使用 Active Directory 组进行 Windows 身份验证

asp.net-mvc - 带有字形图标和不同字体文本的 ASP.NET Actionlink

javascript - 如何一个接一个地渲染多个html文件?

javascript - 组件渲染两次

javascript - 如何检测 jquery ajax 请求是否持续超过 5 秒

c# - 使用 JsonResult 重定向到操作

javascript - 将包含单个数据数组的任务链接(或映射)到任务数组

Javascript:从 JSON 获取数据