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