2016-04-04 4 views
1

ない:上記でJS機能により作成されATTRを引き起こすHTML要素に属性を追加すると、私はIDとHTML要素を有する機能

<div class="button-grp" id="containerButtons"> 
    <label>Selector 1</label> 
    <label>Selector 2</label> 
    <label>Selector 3</label> 
    <div class="time-container"> 
     <input type="number" min="0" max="24" step="1" value="00" id="time-hour" disabled="disabled"> 
     <span>:</span> 
     <input type="number" min="0" max="60" step="1" value="00" id="time-minutes" disabled="disabled"> 
    </div> 
</div> 

labelsdivの数を有するcontainerButtonsいますファイル、buttons.js。

buttons.js:buttons.js以内

function ButtonsPanel() { 

    var that = this; 
    this.buttonsEl = document.createElement('div'); 

    var baseBtn = d3.select(this.buttonsEl); 

    var containerBtns = baseFilter.append('div') 
     .classed({'button-grp': true}) 
     .attr({id:'containerButtons'}); 


    var tmp = containerBtns.append('div') 
     .classed({'time-container': true}); 

    tmp.append('input') 
     .attr({type: 'number', min: '0', max: '24', step:'1', value:'00', id:'time-hour', 'disabled': 'disabled'}); 

    tmp.append('span') 
     .text(':'); 

    tmp.append('input') 
     .attr({type: 'number', min: '0', max: '60', step:'1', value:'00', id:'time-minutes', 'disabled': 'disabled'}); 

    this.timeInputs = tmp; 

    var timeHours = this.timeInputs[0][0].children[0]; 
    // returns <input type="number" min="0" max="24" step="1" value="00" id="time-hour" disabled="disabled"> 
    var timeMins = this.timeInputs[0][0].children[2]; 
    // returns <input type="number" min="0" max="60" step="1" value="00" id="time-minutes" disabled="disabled"> 

} 

、私は負荷toggleVisabilityで呼び出されるメソッドがあります:上記で

ButtonsPanel.prototype.toggleVisability = function() { 

    var that = this; 

    this.visabilityApply('#containerButtons', function(num) { 
     if (!num) { 
      alert(that.timeInputs[0][0].children[0]); 
      that.timeInputs[0][0].children[0].attr({'disabled': true}); 
     } 
     else { 
      alert(that.timeInputs[0][0].children[0]); 
      that.timeInputs[0][0].children[0].attr({'disabled': null}); 
     } 
    }); 
}; 

を、私はエラーを取得:

TypeError: that.timeInputs[0][0].children[0].attr is not a function 

ただし、適用する場合:

that.timeInputs.attr({'disabled': true}); 

上記はうまくいきますが、入力フィールドの無効な属性を切り替える必要があります。私はjQueryを使用しています。

+1

jQueryを使用すると、このコードははるかによく理解できました。 jQueryにもタグが付いているのがわかります。あなたはそれを使いたいですか? – Tushar

+0

あなたのコードベースのフィドルを作成してください。これは理解しやすいです:-) –

+0

@TusharGupta - 私はプロジェクトでjqueryを使用していますが、それほど多くはありません。 –

答えて

4

attr()はjQuery関数であるため、jQuery コンテキストで呼び出す必要があります。あなたのケースでは

:(that.timeInputsはまだjQueryのコンテキストを参照している間に)

that.timeInputs[0][0].children[0] 

は、このようにエラー、attr()機能を公開していないネイティブのDOM要素を返します。

代わりにこれを試してみてください:

$(that.timeInputs[0][0].children[0]).attr({'disabled': true}); 
1

をあなたはまだJavaScriptで行きたい場合は、brakets {}を削除

that.timeInputs[0][0].children[0].setAttribute("disabled", "true"); 
1

ようsetAttribute()を使用することができますwhrap jqueryの内の変数

$(that.timeInputs[0][0].children[0]).attr('disabled': true) 
関連する問題