2013-01-08 28 views
0

我需要一個字段,如果它返回「false」消息,將不允許輸入數據。字段「ponumber」檢查一個數據庫,如果該記錄已經存在,我不希望它允許用戶使用該特定的PO編號。基本上,如果他們離開現場並顯示消息,它就會空白。除了現在允許基於來自checkponumberajax php文件的虛假返回的輸入之外,該腳本工作得很完美。Ajax響應只允許字段中的數據如果爲真

  $(document).ready(function() { 
       var validateponumber = $('#validateponumber'); 
       $('#ponumber').keyup(function() { 
       var t = this; 
       if (this.value != this.lastValue) { 
        if (this.timer) clearTimeout(this.timer); 
        validateponumber.removeClass('error').html('<img src="/images/ajax-loader.gif" height="16" width="16" /> checking availability...'); 

        this.timer = setTimeout(function() { 
        $.ajax({ 
         url: 'includes/checkponumberajax.php', 
         data: 'action=check_ponumber&ponumber=' + t.value, 
         dataType: 'json', 
         type: 'post', 
         success: function (j) { 
         validateponumber.html(j.msg); 
         } 
        }); 
        }, 200); 
        this.lastValue = this.value; 
       } 
       }); 
      }); 

<input type="text" name="ponumber" value="<[email protected]$_REQUEST['ponumber']?>" id="ponumber" /> 

和我checkponumber.php文件的回報這樣

if ($records != 0) { 
    $response = array(
    'ok' => false, 
    'msg' => "The selected PO# is not available"); 
} else { 
    $response = array(
    'ok' => true, 
    'msg' => "This PO# is free"); 
} 

編輯:解決了!

最後我感謝使用這種簡單地從空白匹配假現場,一旦達到一個值

if(j.ok != true){$("#ponumber").attr("value", "");} 

回答

1

嘗試使用此方法@bourch:

if(j.ok != true){$("#ponumber").attr("disabled", "disabled"); 
} 
+0

關閉,但只要你輸入一個存在的值然後禁用該字段,我不能再訪問它的LOL。我將「禁用」,「禁用」更改爲「值」,「」,只是在達到錯誤時將值清空。我用解決方案更新了我的原始問題。謝謝 –