我使用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; }
答案 0 :(得分:1)
我发现代码有问题,你需要在获取值时使用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
答案 1 :(得分:0)
您的说明缺少一些步骤。无论如何,我会尝试给出一些观点,希望它会有所帮助。 控件的初始值是一个空字符串(据我所知),因此比较它们将给出两个空字符串的字符串比较。 请注意,在某些版本的FireFox中,最终控件的值在失去焦点后设置,因此有时您获得的值是onchage事件中的旧值...