2009-10-24 132 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; }