2015-04-06 143 views
-3

我有下面的代碼後開啓/關閉:使文本框變灰數據保存到數據庫(AJAX/PHP)

<input type="text" value="name" id="name" /> 
<input type="button" id="button" value="Click Me" /></p> 

<script> 
    $("#button").click(function() { 
     var text = $("#name").val(); 
     $.ajax({ 
      url: 'page.php', 
      type: "POST", 
      data: { 
       text: text 
      }, 
      success: function(response) { 
       alert("inserted"); 
      } 
     }); 
    }); 
</script> 

和現在的一切工作正常,但點擊該按鈕後,我得到了彈出味精(警報)表示數據已插入。而不是我想灰色的文本字段 - 有可能嗎?

+0

您是否嘗試過加入'disabled'屬性來輸入你的成功回調? – Joe

+0

下次請嘗試谷歌這一點。 「jquery禁用輸入」會給你相同的答案 –

回答

1

您可以使用prop()禁用文本輸入。這在大多數瀏覽器中也會變成灰色。試試這個:

success: function(response) { 
    $('#name').prop('disabled', true); 
} 
+0

謝謝,這解決了我的問題! – randomuser2

1
function disable(){ 
    $("#name").prop("disabled",true); 
} 
0

嘗試後ajax成功回調這樣,

$("#name").attr('readonly', 'true'); // mark it as read only 
    $("#name").css('background-color' , '#DEDEDE'); // change the background color