2009-10-24 92 views
0

将ASP.net MVC表单中文本字段的颜色更改为最简单的方式是根据刚输入的表单提交的之前的值进行更改。ASP .NET ajax字段验证

要使用的颜色由数据库查询返回。

例如:

查询返回红如果数字在字段中输入较低则在DB项目注册数量。

感谢

回答

1

你可以像在下面的例子中,使用jQuery:

//bind a function to the blur even of the text field 
$("#the-imput-control").blur(function() { 
    var value = this.val(); 
    //send the value to the server and then process the result. 
    $.getJSON("yourUrl", { key: value }, function(data) { 
     //return a json object with a property "color" 
     //and use its value to set the text field's color 
     $("#the-imput-control").css("color", data.color); 
    }); 
    //you can of course use another ajax function depending on your needs. 
}); 
0

你想有一个事件的onclick提交按钮或改变量来检索量的产品。

您也可以在开始时加载颜色,但在用户输入期间库存可能会更改。

例如

  1. 用户加载一个订单,数量的股票:4,也许你设置颜色为橙色,因为它是低...
  2. 用户填写表单,其他一些用户订购总共有3件,剩余1件
  3. 用户想订购2件商品。在
  4. 用户点击提交,你有你的事件检查的数量和显示信息/更改文本框

的颜色,我明白你不会有一个回传,如果在数据库中金额低于其量低有序....但考虑到用户可能没有打开JavaScript,你也应该在服务器端实现。

就个人而言,我只是在服务器端做,因为客户端只是一个额外的功能,你不能依赖。

0

如果它没有施加任何安全风险,最好缓存产品的可用数量,而不是去服务器进行验证。这样,用户界面的响应就会更加灵敏 - 用户体验会更好。所以,基本上,

// JS 
var availableQuantities = [10, 15, 30]; 
$(".the-imput-control").blur(function() { 
    var value = $(this).val(); 

    var productIndex = $(this).parent().children().index(this); // sort of 

    $(this).toggleClass('invalid', (availableQuantities[productIndex] < value)); 
}); 

// CSS 
.invalid { color: red; }