2013-01-12 5 views
5

ho un tipo di inputsola lettura attribuiscono alternare

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

ho una casella di controllo che, quando sarà cliccato farà sì che il testo sia modificabile ...

<input type="checkbox" name="ch" id="ch" onclick="check()" /> 

come cud posso risolvere il problema? ??

Io uso una sceneggiatura

function check() 
{ 
if(document.getElementById('ch').checked=true) 
    document.getElementById('ch').readonly=false; 

} 
+2

perché è questo domanda taggata jQuery? Stai usando jQuery? –

+0

Qual è il problema con il codice corrente? –

risposta

2

Prova questa

function check() { 
    document.getElementById('name').readOnly = 
        !document.getElementById('ch').checked; 
} 
4

Questa è un'alternativa più breve (supponendo che si aggiungono gli ID adeguati per i controlli):

$("#ch").change(function() { 
    $("#name").prop("readonly", !$(this).is(":checked")); 
});