Javascript 如果使用javascript在“选择”下拉列表中选择“选项”,如何显示弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7649072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to show popup if select "option" in "select" dropdown using javascript?
提问by Pandurangappa
If I select
Option1
it has to show popupIf I select
Option2
it has to show message below select box in pageIf I select
Option3
it has to show iframe in the page
如果我选择
Option1
它必须显示弹出窗口如果我选择
Option2
它必须在页面中的选择框下方显示消息如果我选择
Option3
它必须在页面中显示 iframe
How to achieve the above?
如何实现上述目标?
采纳答案by Punit
<script>
$(document).ready(function(){
$(':input#mysel').live('change',function(){
var sel_opt = $(this).val();
alert(sel_opt);
if(sel_opt==1)
{
your code;
}
else if(sel_opt==2)
{
your code;
}
else if(sel_opt==3)
{
your code;
}
});
});
</script>
<select id="mysel">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>`
回答by Sedat Ba?ar
I wrote on jsfiddle for you. http://jsfiddle.net/BgGTH/1/
我在 jsfiddle 上为你写的。 http://jsfiddle.net/BgGTH/1/
回答by Shadow Wizard is Ear For You
Just handle the onchange
of the drop down:
只需处理onchange
下拉菜单:
$("#DropDown1").bind("change", function() {
var value = this.value;
switch (value) {
case "Value1":
window.open("MyPage.html", "myPage");
break;
case "Value2":
$("#MyMessage").show();
break;
case "Value3":
$("#MyFrame").show();
break;
}
});
This should point you in the correct direction.
这应该为您指明正确的方向。
回答by HymanJoe
use jQuery's .change()
使用 jQuery 的 .change()
example:
例子:
html:
html:
<select name="sel1" id="sel1">
<option value="1">popup</option>
<option value="2">message</option>
<option value="3">iframe</option>
</select>
javascript:
javascript:
jQuery('#sel1').change(function() {
if(jQuery(this).val() == "1") {
//popup code
}
});
Of course this example only shows the first case, and you could use a switch
for the three (or more) options.
当然,此示例仅显示第一种情况,您可以将 aswitch
用于三个(或更多)选项。
回答by nnnnnn
I'm assuming by "if select “option” in “select” dropdown using javascript" you mean "when the user selects a particular option I want to use JavaScript to make something else happen".
我假设“如果使用 javascript 在“选择”下拉列表中选择“选项””,您的意思是“当用户选择特定选项时,我想使用 JavaScript 来做其他事情”。
This should get you started:
这应该让你开始:
$("#IDofYourSelect").change(function() {
switch ($(this).val()) {
case "Option1":
alert("I don't know what you mean by 'popup', but do that here.");
break;
case "Option2":
// show message below select
break;
case "Option3":
// do your iframe thing
break;
}
});
If you want something more specific you should make your question (a lot) clearer.
如果你想要更具体的东西,你应该让你的问题(很多)更清楚。
回答by dmackerman
I assume you mean this:
我想你的意思是:
HTML:
HTML:
<select id="selection">
<option value="1">Option 1</option>
<option value="2">Option 1</option>
<option value="3">Option 1</option>
</select>
Javascript
Javascript
$(function() {
$("#selection").change(function() {
var val = $(this).val();
if (val == 1) {
// ...
}
});
});