0

複数のチェック ボックス コントロールをクリックしたときに関数を呼び出したいのですが、

$('input[name=filt_box_gen]').click(function() {  
nodescheckclick(this,1);
})  

コントロール名の配列を作成しました。

var nodesfilterlist = ['input[name=filt_box_gen]','input[name=filt_box_que]','input[name=filt_box_ans]'];  

...しかし、関数を3回、それぞれの場合に1回呼び出して機能させることができるだけです...

$(nodesfilterlist[0]).click(function() {
nodescheckclick(this,0);
});

$(nodesfilterlist[1]).click(function() {
nodescheckclick(this,1);
});

$(nodesfilterlist[2]).click(function() {
nodescheckclick(this,2);
});  

nodescheckclik配列を繰り返し処理して、関数を 1 つだけ呼び出すにはどうすればよいですか?

ありがとう!

4

1 に答える 1

1

コメントで述べたように、これは d3.js よりも JavaScript または jQuery に関する質問です。

forEach()配列には組み込み関数を使用できます。すなわち:

nodesfilterlist.forEach(function(d,i){
    $(d).click(function(){nodescheckclick(this, i)})
})

d配列内の要素であり、i配列内の要素のインデックスです。


さて、実際にd3を使用してnodefilterlistいて、あなたが扱っているデータである場合。次に、次のようなものがあります。

var nodes = d3.select("#checkBoxContainer") // Container for checkBoxes
    .selectAll("#checkBox") //name it as you wish, will create as many elements as there is in the data
    .data(nodefilterlist) //append the data you want to use
    //.attr("color", "blue") // would set attributes to data
    .on("click", nodeschecklist) //perform action on data

両方のバージョンが同じ署名を持っているため、 nodeschecklist期待どおりに自動的に翻訳されるため ( ) 、呼び出しについてさらに記述する必要はありません。.on("click", function(d, i){nodeschecklist(d,i)})

于 2013-04-21T07:23:08.193 に答える