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()
謝謝!它現在完美。 –
太棒了!很高興我能幫忙。 – Serlite