2013-07-04 66 views
1

如果從選擇輸入中選擇「現金」付款,我寫了這段代碼以「禁用」文本輸入。如果選擇「信用卡」付款,我還想「啓用」文本輸入。但我甚至無法使用JavaScript完成啓用。JavaScript:根據從「選擇輸入」中選擇的值啓用/禁用「文本輸入」

<input type="text" readonly="true" class="credit_card_number" id="inputWarning" name="credit_card_number"/> 

HTML:

<div class="control-group"> 
<label for="payment_type" class="control-label"><i class="icon-book"></i> Payment Type</label> 
<div class="controls"> 
<select size="1" id="payment_type" name="payment_type" onchange="enableElements();"> 
    <option value="cash" id="cash" selected="selected">Cash</option> 
    <option value="amex" id="credit_card">American Express</option> 
    <option value="master" id="credit_card">Master Card</option> 
    <option value="visa" id="credit_card">Visa</option> 
</select> 
</div> 

的JavaScript:

<script> 
function enableElements() 
{ 
document.getElementById('inputWarning').disabled=false; 
} 
</script> 
+0

嘗試'$( '#inputWarning')ATTR( '禁用', '假');' –

+0

這裏只有一個文本輸入。 –

+2

有沒有必要涉及jQuery這麼簡單的事情.. – lifetimes

回答

3

FIDDLE HERE

這將工作。但我認爲,我建議從只讀變爲attr:disabled,對用戶來說更加清晰。在這種情況下,請檢查here

腳本/ jQuery的

function enableElements() { 
if ($("#payment_type").val() == 'cash') { 
    $("#inputWarning").prop('readonly', true); 
    console.log('cash'); 
} 

if ($("#payment_type").val() == 'amex' || $("#payment_type").val() == 'master' || $("#payment_type").val() == 'visa') { 
    $("#inputWarning").prop('readonly', false); 
    console.log('other'); 
} 
}; 
+0

這工作順利。謝謝 :) –

0
document.getElementById('inputWarning').disabled="" 

http://jsfiddle.net/rKQjp/

通過將disabled屬性設置爲「disabled」將元素禁用,如果將此屬性設置爲空字符串,則會再次啓用該元素。

0

試一下:

function enableElements() 
{ 
    if($('#payment-type').val() == "cash"){ 
     $('#inputWarning').attr('disabled', 'true'); 
    } 
    else $('#inputWarning').attr('disabled', 'false'); 
} 
+0

PO沒有問及使用jQuery .. – Paritosh

+0

這個問題有一個jQuery標籤,所以我用jQuery。 –

+0

正確! :) u're – Paritosh

0
function enableElements() 
{ 
    var e = document.getElementById("payment_type"); 
    var str = e.options[e.selectedIndex].value; 
    if(str == 'Cash') 
     document.getElementById('inputWarning').setAttribute("readonly", "true"); 
    else if(str == 'amex' || str == 'master' || str == 'visa') 
     document.getElementById('inputWarning').setAttribute("readonly", "false"); 
} 

注意:有問題,相同的id是有3個選項。使用不同的屬性使用jQuery識別

function enableElements() 
{ 
    var str = $("#payment_type").val(); 
    if(str == 'Cash') 
     $('#inputWarning').attr('readonly', true); 
    else if(str == 'amex' || str == 'master' || str == 'visa') 
     $('#inputWarning').removeAttr('readonly'); 
} 
+0

在OP的輸入中只有attr,所以它不可寫。 – Sergio

+0

「注意:有問題,同樣的id有3個選項。使用不同的屬性來標識」 @Paritosh對不起。將他們改爲「班級」。我可能在使用Twitter引導時忘記了基本知識。固定! :) –

+0

我想我可能會堅持@塞爾吉奧的解決方案。它首先起作用。現在定製它。不管怎麼說,還是要謝謝你。:) –

0

試試這個腳本。我用過jQuery。確保添加jquery庫。

<script> 
function enableElements() 
{ 
var type = $('#payment_type').val(); 
if(type == 'amex' || type == 'master' || type == 'visa') 
{ 
    $("#inputWarning").attr("readonly", true); 
} 
else 
{ 
    $("#inputWarning").attr("readonly", false); 
} 

</scrip