【问题标题】:How to get access to the selected element?如何访问选定的元素?
【发布时间】:2017-08-02 03:44:01
【问题描述】:

我使用 d3.js 并创建了一个“选项”窗口,它允许我在一些元素的索引 i 之间进行选择,比如一个长度为 4 个元素的数组。用户 Andrew Reid 在另一个线程中帮助编写了此代码,因此所有功劳归于他:

var data = [10,20,30,40];

var color = d3.schemeCategory10; // color array built in

//// Add the select and options:
var select = d3.select('body')
  .append('select')
  .on('change',function() { update(this.value) });

var start = select.append('option')
  .html("select: ");

var options = select.selectAll('.option')
  .data(data)
  .enter()
  .append('option')
  .attr('class','option')
  .attr('value',function(d,i) { return i; })
  .html(function(d,i) { return i; });



//// Add the circles (and svg)
var svg = d3.selectAll('body')
  .append('svg')
  .attr('width',500)
  .attr('height',200);

var circles = svg.selectAll('circle')
  .data(data)
  .enter()
  .append('circle')
  .attr('cx',function(d,i) { return i * 30 + 50; })
  .attr('cy',50)
  .attr('r',10)
  .attr('fill',function(d,i) { return color[i]; });


// Update everything:
function update(i) {
  data.splice(i,1); // remove that element.

  // Update and remove option from the select menu:
  options.data(data).exit().remove();

  // Remove that circle:
  circles.data(data).exit().remove(); 

  circles.attr('cx',function(d,i) { return i * 30 + 50; })
    .attr('fill',function(d,i) { return color[i]; });

  // reset the select menu:
  start.property('selected','selected');
}

.html(function(d,i) { return i; }); 行的帮助下,“选项”窗口会显示索引并允许我单击它。

我的目标:

例如,“选项”窗口会显示索引的编号: 0、1、2、3。 现在我点击索引“2”,我想在其他功能中使用该索引“2”。我的问题是,我怎样才能将点击的数字保存在变量a 中,并在拼接函数中使用这个变量:

data.splice(i, a) 

当前代码中拼接函数为data.splice(i,1)

感谢您的帮助。

【问题讨论】:

  • 你在哪里执行这个array.splice(i, a)?是在选项的点击事件上吗?这个问题看起来像XY Problem
  • 嘿,马克,这不是选项的点击事件。它是一个单独的外部函数。
  • @DerickKolln 如果不了解事件处理程序如何调用其他函数,就不可能为您提供帮助。请发布MCVE(强调最低!)代码。
  • 谢谢。但我不关注这个:它是切片还是拼接? (它们不一样)。而且您已经正确传递了索引i!那个数字a 不是索引。
  • 您是否希望每次更改都从一个选择菜单中获取两个值 (i,a)(如果您选择的任何值超过数据数组长度的一半,这可能会出现问题),或者只需从选择菜单中获取一个值或索引并将其存储在一个变量中以供以后使用(“现在我单击索引“2”并且我想在其他函数中使用该索引“2””)?使用后一种选项,是否存在关联的函数触发器?为我的密集道歉,我会把它归咎于与夏令时有关的事情。

标签: javascript arrays variables d3.js options


【解决方案1】:

您可以将选项文本从 onchange 函数传递给更新函数:

this.options[this.selectedIndex].text

所以你最终会得到:

var select = d3.select('body')
  .append('select')
  .on('change',function() { update(this.value, this.options[this.selectedIndex].text) }); // note: since you are setting value equal to the index, you can also use this.value instead of this.selectedIndex

function update(i, a) {
  data.splice(i,a);
  ...
}

这是如何工作的:在您的代码中,您使用 d3 附加一个 HTML select 元素 (Select object),其中包含多个 HTML option 元素 (Option object)。

然后在select 元素上,onchange 函数(这是select 元素的standard event)然后将select 元素传递给它的函数处理程序,因此在该函数中this 表示实际的select 元素,因此this.value (Select value property) 对应于select 元素的值(即select 元素中当前选定的optionvalue)。

select 元素的options 属性,即this.options (Select options collection),为您获取该select 中所有option 元素的列表。使用select 元素的selectedIndex 属性,即this.selectedIndex (Select selectedIndex property),您可以从列表中获取当前选中的option 元素为this.options[this.selectedIndex]

由于this.options 集合是选项元素 (Option object) 的列表,要获取该option 的实际文本,您可以在其上使用.text。这就是你最终得到this.options[this.selectedIndex].text的方式。

【讨论】:

  • 我认为您需要将选项附加到您的选择中。
  • 嗯,是的,我知道你是对的。当您尝试使用 selectedIndex 时发生了什么?您是否遇到某种错误?
  • 好的,我想我找到了问题!我的回答有点不对劲。应该是this.options[selectedIndex].text
  • 没问题!我用.text 修复更新了我的答案,并解释了它是如何工作的。如果它回答了您的问题,如果您可以通过单击旁边的复选标记将我的答案标记为已接受,那就太好了:)
  • 谢谢!它实际上只是 HTML 选项对象的内置属性 - 请参阅 https://www.w3schools.com/jsref/prop_option_text.asp
猜你喜欢
  • 2016-07-05
  • 2018-07-05
  • 2019-07-15
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 2017-05-11
相关资源
最近更新 更多