2015-06-17 68 views
1

我有一個文本字段兩個單選按鈕在選定的單選按鈕上改變文本字段類型爲數字

我想

  • Option A選擇= 無能爲力

  • Option B選擇= 文本輸入類型更改爲number

我下面要發生有一個概率當選擇Option B後,用戶選擇Option A,lem將輸入類型恢復爲text。字段類型保持爲number

如何在Option A重新選擇時恢復到原始狀態?

Demo

$(document).ready(function(){ 
    $("input[name='searchby']").live("change", function(){ 
     if ($(this).val() == "tag") { 
      //clear the text field 
      $('#textvalue').val(""); 

      if($('#c_tag').is(':checked')) {    
       $("#textvalue").keypress(function (e) { 
//if the letter is not digit then display error and don't type anything 
       if (e.which != 8 && e.which !== 0 && (e.which < 48 || e.which > 57)) { 
//display error message 
       $("#errmsg").html("Digits Only").show().fadeOut("slow"); 
       return false; 
       } 
       }); 
      } 
     } 
     else if ($(this).val() == "name") { 
      //clear the text field 
      $('#textvalue').val("");   
     } 
    }); 
}); 

編輯: 上面的代碼是使用jQuery v1.6.3

jQuery v1.7+,請更改以下已過時事件:.live().on()

回答

1

可以使用.unbind()功能從#textvalue刪除任何按鍵處理程序,即:

$('#textvalue').unbind("keypress"); 

這裏有一個modified JSFiddle

希望這有助於!如果您有任何問題,請告訴我。

+0

謝謝!它現在完美。 –

+0

太棒了!很高興我能幫忙。 – Serlite

相關問題