简体   繁体   中英

Disable textbox on checkbox checked

I'm trying to disable an input field when a checkbox is (not) checked.
I know that there are already answers to this question but what they said didn't seem to work for me.
So what i tried was this:

function disableWarentarifByAusland() {
var isChecked = $("#ausland").is(":checked");
var warentarif = $("#ArtikelTabelle .warentarif");
alert(warentarif);
for (var ii = 0; warentarif.length >= ii  ; ii++) {
    if (!isChecked) {
        $(warentarif[ii]).prop("disbled", true);
        //$(warentarif[ii]).val(true);
    } else {
        $(warentarif[ii]).prop("disbled", false);
        //$(warentarif[ii]).val(false);
    }
}
}

Using it on this table:

   $("#add_row").click(function () {
    lZeile++;
    $("#ArtikelTabelle > tbody").append('<tr id="reihe' + lZeile + '">' +
        '<td rowspan="2"><b>' + (lZeile + 1) + '</b></td>' +
        '<td><input class="form-control" id="ccTabelle_' + lZeile + '__ccArtikelNr" name="ccTabelle[' + lZeile + '].ccArtikelNr" type="text" value="" placeholder="Artikelnummer"/></td>' +
        '<td><input class="form-control warentarif" data-val="true" data-val-required="Das Feld &quot;Warentarif-Nr&quot; ist erforderlich." id="ccTabelle_' + lZeile + '__ccWarentarifNr anzahl" name="ccTabelle[' + lZeile + '].ccWarentarifNr" type="text" value="" placeholder="Warentarifnummer"/></td>' +
        '<td><input class="form-control anzahl" data-val="true" data-val-number="Das Feld &quot;Anzahl&quot; muss eine Zahl sein." data-val-required="Das Feld &quot;Anzahl&quot; ist erforderlich." id="ccTabelle_' + lZeile + '__ccAnzahl" name="ccTabelle[' + lZeile + '].ccAnzahl" type="text" placeholder="Anzahl" /></td>' +
        '<td><input class="form-control preis" data-val="true" data-val-number="Das Feld &quot;Einzelpreis&quot; muss eine Zahl sein." data-val-required="Das Feld &quot;Einzelpreis&quot; ist erforderlich." id="ccTabelle_' + lZeile + '__ccEinzelpreis" name="ccTabelle[' + lZeile + '].ccEinzelpreis" type="text" placeholder="Einzelpreis"/></td>' +
        '<td rowspan="2"><a class="btn btn-default delete_row" data-rowid="' + lZeile + '">Artikel löschen</a></td>' +
        '</tr>' +
        '<tr id="text' + lZeile + '">' +
        '<td colspan="4"><textarea class="form-control" data-val="true" data-val-required="Das Feld &quot;Inhaltsbeschreibung&quot; ist erforderlich." id="ccTabelle_' + lZeile + '__ccInhaltsbeschreibung" name="ccTabelle[' + lZeile + '].ccInhaltsbeschreibung" placeholder="Inhalt">' +
        '</textarea></td></tr>');

I'm using the content of the append to create the table on document ready.
You can see in the jquery that I used alert() and val() to test if this function is executed and it does. I can change the content in the input field and the alert goes off, but the prop doesn't change or even appear in the code, after checking.
What is my problem here ?

You should fix the spelling of you ' disabled ' property name:

$(warentarif[ii]).prop("disabled", true);

$(warentarif[ii]).prop("disabled", false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM