23

ボタンを無効にするために JavaScript を使用しています。IE では正常に動作しますが、FireFox と chrome では動作しません。私が取り組んでいるスクリプトは次のとおりです。

function disbtn(e) { 
    if ( someCondition == true ) {
       document.getElementById('btn1').disabled = true;
    } else {
       document.getElementById('btn1').disabled = false;
    }

そして、私のhtmlには次のものがあります:

<input type="button" id="btn1" value="submit" />
4

7 に答える 7

60

setAttribute()およびremoveAttribute( )を使用します

function disbtn(e) { 
    if ( someCondition == true ) {
       document.getElementById('btn1').setAttribute("disabled","disabled");
    } else {
       document.getElementById('btn1').removeAttribute("disabled");
    }
}

デモを見る

于 2012-07-31T10:01:40.620 に答える
6

disabled属性を直接設定してみてください。

if ( someCondition == true ) {
   document.getElementById('btn1').setAttribute('disabled', 'disabled');
} else {
   document.getElementById('btn1').removeAttribute('disabled');
}
于 2012-07-31T10:01:22.413 に答える
2

getElementByIdのブラウザサポートには常に奇妙な問題があります。代わりに次を使用してみてください。

// document.getElementsBySelector are part of the prototype.js library available at http://api.prototypejs.org/dom/Element/prototype/getElementsBySelector/

function disbtn(e) { 
    if ( someCondition == true ) {
        document.getElementsBySelector("#btn1")[0].setAttribute("disabled", "disabled");
    } else {
        document.getElementsBySelector("#btn1")[0].removeAttribute("disabled");
    }    
}

または、これを簡単に実行できるjQueryを採用します。

function disbtn(e) { 
    if ( someCondition == true ) {
        $("#btn1").attr("disabled", "disabled");
    } else {
        $("#btn1").removeAttr("disabled");
    }    
}
于 2012-07-31T10:01:15.027 に答える
0

一部の特定のブラウザーでは、一部の JavaScript 関数が機能しないことがあります。さまざまなブラウザー要件に対応しながら、正規化された JS を提供する JQuery の使用を開始することをお勧めします。

$('#btn1').each(function(){
    this.disabled = false;
});
于 2012-07-31T10:02:14.833 に答える
0

別の選択肢:

document.formname.elementname.disabled=true

FF と IE で動作します。:)

于 2016-04-04T09:22:17.573 に答える
-2

ネイティブ (ブール) プロパティのサポートと、次のような強力な構文に忠実であり続けます。

[elem].disabled = 条件 ? 真/偽; //終わり!

そして、私たち自身の優れた共同コーディング経験のために、他の人にもそれをサポートするよう主張してください.

于 2014-02-16T13:10:25.063 に答える