【问题标题】:How To Change Iframe source Based On Button State Using Javascript如何使用 Javascript 根据按钮状态更改 iframe 源
【发布时间】:2017-05-12 07:42:29
【问题描述】:

我正在尝试为网页创建一系列按钮,这些按钮的功能类似于切换按钮(活动/非活动),并会根据当前选择的按钮组合更改 iframe 的源。因此,例如,如果我想让某人可视化进来的产品:

三种颜色(红、绿、蓝)

三种材料类型(金属、塑料、玻璃)

三种尺寸(小、中、大)

我碰巧有一个单独的网页,用于根据当前选择(在本例中为 27 种可能的组合)显示的每个选项,我将如何做呢?抱歉,我对此不是很有经验,但我正在努力学习。

//Would I set the initial variables like this?
var Red = true;
var Blue = false;
var Green = false;
var Metal = true;
var Plastic = false;
var Glass = false;
var Small = true;
var Medium = false;
var Large = false;
...

//Then set the initial state of each button? (Sorry, not sure what this would look like.)
btnRed.active;
btnMetal.active;
btnSmall.active;
...

//Then make a conditional statement to make sure two variables and two button states cannot be true at the same time within each of the three categories? (Again, sorry not sure what some of this code would look like.)
if (btnRed.active){
  Red = true;
  Blue = false;
  Green = false;
  btnBlue.active = false;
  btnGreen.active = false;
} else if (btnBlue.active){
  Red = false;
  Blue = true;
  Green = false;
  btnRed.active = false;
  btnGreen.active = false;
} else if (btnGreen.active){
  Red = false;
  Blue = false;
  Green = true;
  btnBlue.active = false;
  btnRed.active = false;
}
...

//Then set the iframe source based on the variable combination?
if (Red = true && Metal = true && Small = true){
  document.getElementById('myIframe').src = "http://somesite.com";
}
...

感谢任何帮助。谢谢!

【问题讨论】:

  • 让我们为颜色类型、材料类型等赋予代码值。例如说红色为 0,绿色为 1,蓝色为 2,金属为 0,塑料为 1,小号为 0,中号为 1 等等。现在,制作一个包含站点名称的字符串数组。对于 red(0)、plastic(1)、medium(1),将该站点名称存储在 sites[0][1][1]。所以现在,你可以通过sites[color-code][material-code][size-code]找到src名称。
  • 我建议你改用单选按钮。

标签: javascript jquery html iframe


【解决方案1】:

我假设您正在使用单选按钮。

$('input[type="radio"]').on("change", setIframe);

var setIframe = function() {
  var url;
  //computation of url
  $('iframe').attr("src", url);
}

这是完整的工作代码:Working Code

【讨论】:

  • 谢谢拉胡尔。感谢您花时间创建一个我可以从中学习的工作示例。我将实现这个示例并做一些自定义 CSS 以使 raidio 按钮看起来不错。
猜你喜欢
  • 1970-01-01
  • 2015-06-10
  • 2014-08-30
  • 1970-01-01
  • 2019-12-29
  • 2021-07-19
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多