2016-02-01 20 views
0

我在這裏有一個表單,其中用戶可以選擇單選按鈕來選擇預定義的金額選項,或者按下文本框(焦點)來選擇自定義金額。下面的JavaScript的目的是在選擇預定義數量時清除文本框。它還允許用戶點擊文本框(onfocus)在CP_otheramount字段中輸入自定義金額(同時使用單選按鈕作爲後備)。對焦選擇文本框時不工作

除了聚焦之外,這一切似乎都在起作用。它適用於負載...但嘗試這種情況下:

加載頁面,單擊文本框內,但然後決定通過選擇值3單選按鈕(64),然後決定改變主意,然後決定按在onfocus文本框中再次顯示數量......它被禁用並阻止您在內部點擊或寫入數字!

任何人都可以在這裏看到問題嗎?它很奇怪,因爲它在Stackoverflow上工作而不是在現場。任何幫助將非常感激。

這裏是已經創建至今:

function activate() { 
    $('#other').prop('checked', true); 
    $('#other').trigger('click'); 
    $('#theamount').focus(); 
} 
$('input[name="am_payment"]').on('click', function() { 
    if ($(this).val() === '') { 
    $('input[name="CP_otheramount"]').val(''); 
    $('#theamount').removeAttr("disabled"); 
    } else { 
    $('#theamount').prop("disabled", "disabled"); 
    $('input[name="CP_otheramount"]').val(''); 
    } 
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

<input type="radio" name="am_payment" value="3" checked="checked"> <strong>64</strong> 

<input type="radio" name="am_payment" value="11" checked="checked"> <strong>100</strong> 

<input type="radio" name="am_payment" value="32" checked="checked"> <strong>250</strong> 

<input type="radio" value="" name="am_payment" id="other"> 
<label>Other</label> 

<span onclick="activate();"><input type="text" name="CP_otheramount" value="" id="theamount" disabled="disabled"/></span> 
+0

不能點擊頁面加載文本框,因爲你已經添加的屬性被禁用,因此,除非點擊其他單選按鈕,它沒有得到啓用的 – brk

+0

可能的複製[清除文本字段時,在Javascript中選擇一個單選按鈕] (http://stackoverflow.com/questions/34916173/clearing-a-textbox-field-when-a-radio-button-is-selected-in-javascript)和[清除文本框時 - 任何其他-radio-buttons-are-selected](http://stackoverflow.com/questions/34938031/clearing-a-textbox-when-any-other-radio-buttons-are-selected) –

+0

如果文本框被禁用,它可以沒有焦點。 – nnnnnn

回答

0

與您的代碼的問題是,你正在檢查沒有取消選中它們每一個單選按鈕。

function activate() { 
    // $('#other').prop('checked', true); Remove this line; 
    $('#other').trigger('click'); 
    $('#theamount').focus(); 
} 
$('input[name="am_payment"]').on('click', function() { 
    if ($(this).val() === '') { 
    $('input[name="CP_otheramount"]').val(''); 
    $('#theamount').removeAttr("disabled"); 
    } else { 
    $('#theamount').prop("disabled", "disabled"); 
    $('input[name="CP_otheramount"]').val(''); 
    } 
}); 

而且還

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

<!-- Only check one, or none by Default --> 
<input type="radio" name="am_payment" value="3" checked = "checked"> <strong>64</strong> 

<input type="radio" name="am_payment" value="11"> <strong>100</strong> 

<input type="radio" name="am_payment" value="32"> <strong>250</strong> 

<input type="radio" value="" name="am_payment" id="other"> 
<label>Other</label> 

<span onclick="activate();"><input type="text" name="CP_otheramount" value="" id="theamount" disabled="disabled"/></span> 

UPDATE:

它是爲我工作,但我不知道爲什麼它不爲你工作,所以我改寫了functions..See如果這對你有用。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

<input type="radio" name="am_payment" value="3" checked = "checked"> <strong>64</strong> 

<input type="radio" name="am_payment" value="11"> <strong>100</strong> 

<input type="radio" name="am_payment" value="32"> <strong>250</strong> 

<input type="radio" value="" name="am_payment" id="other"> 
<label>Other</label> 

<span id="toggle"><input type="text" name="CP_otheramount" value="" id="theamount" disabled="disabled"/></span> 

<script> 
    function activate(){ 
     $('#theamount').attr('disabled',false); 
     $('#theamount').focus(); 
     $('#other').click(); 
    } 

    function deactivate(){ 
     $('#theamount').val(''); 
     $('#theamount').attr('disabled',true); 
    } 

    $('#toggle').click(function(){ 
     activate(); 
    }); 
    $('input[name="am_payment"]').change(function(){ 
     if($(this).val() != ""){ 
      deactivate(); 
     }else{ 
      activate(); 
     } 
    }); 
</script> 
+0

我已經完成了上述操作,但如果您首先爲值100(或64或250)選擇一個單選按鈕,然後再單擊或按下文本框內的其他按鈕,文本框仍不會讓您單擊它。 – ste

+0

@ste我重寫了這些函數......看看這個是否可行 –