简体   繁体   English

如何使用Javascript根据按钮状态更改iframe源

[英]How To Change Iframe source Based On Button State Using Javascript

I am trying to create a series of buttons for a web page that would function like toggle buttons (active/inactive) and would change the source of an iframe based on which combination of buttons are currently selected. 我正在尝试为网页创建一系列按钮,这些按钮的功能类似于切换按钮(活动/非活动),并根据当前选择的按钮组合来更改iframe的来源。 So, for example, if I want someone to visualize a product that comes in: 因此,例如,如果我要某人可视化进来的产品:

three colors (Red, Green, Blue) 三种颜色(红色,绿色,蓝色)

three material types (Metal, Plastic, Glass) 三种材料类型(金属,塑料,玻璃)

three sizes (Small, Medium, Large) 三种尺寸(小,中,大)

and I happened to have a separate webpage for each option that I want to show based on the current selections (27 possible combinations in this case), how would I go about doing that? 并且我碰巧有一个要基于当前选择显示的每个选项单独的网页(在这种情况下为27种可能的组合),我该怎么做呢? Sorry, I am not very experienced with this, but I am trying to learn. 抱歉,我对此并不十分了解,但是我正在尝试学习。

 //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"; } ... 

Any help is appreciated. 任何帮助表示赞赏。 Thank you! 谢谢!

I am assuming you are using radio buttons. 我假设您正在使用单选按钮。

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

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

Here is the complete working code : Working Code 这是完整的工作代码: 工作代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM