jQuery if else 和 select 标签中的选定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4320376/
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
jQuery if else and selected value from select tag
提问by aherlambang
Why can't I get the value selected from my input, am I doing something wrong?
为什么我不能从我的输入中获得选择的值,我做错了什么吗?
if($('#target option:selected').text() == "add")
$("#add").show(selectedEffect, options, 500, callback );
else if ($('#target option:selected').text() == "exit")
$("#exit").show(selectedEffect, options, 500, callback );
else if ($('#target option:selected').text() == "refuse")
$("#refuse").show(selectedEffect, options, 500, callback );
else
alert('test');
回答by Vlad.P
var target = $('#target option:selected').val();
if(target == "add")
$("#add").show(selectedEffect, options, 500, callback );
else if ($('#target option:selected').text() == "exit")
$("#exit").show(selectedEffect, options, 500, callback );
else if ($('#target option:selected').text() == "refuse")
$("#refuse").show(selectedEffect, options, 500, callback );
else
alert('test');
Check it out here http://api.jquery.com/val/
回答by John Hartsock
If you want the value use .val()
method.The .text()
methodreturns the inner text of the element selected. The .val()
method will return the value attribute of the element selected. It is also of note that when using val() method on a select element. You do not have to get the selected option in the selector, you can simply call val() on the select element itself to get the selected value.
如果你想要值使用.val()
方法。该.text()
方法返回所选元素的内部文本。该.val()
方法将返回所选元素的 value 属性。还需要注意的是,在 select 元素上使用 val() 方法时。您不必在选择器中获取选定的选项,您只需在 select 元素本身上调用 val() 即可获取选定的值。
$('#target').val();
If the value attribute of the selected option is what you want. Then from your example this will work.
如果所选选项的 value 属性是您想要的。然后从您的示例中,这将起作用。
switch ($('#target').val()) {
case "add":
$("#add").show(selectedEffect, options, 500, callback );
break;
case "exit":
$("#exit").show(selectedEffect, options, 500, callback );
break;
case "refuse":
$("#refuse").show(selectedEffect, options, 500, callback );
break;
default
alert('test');
break;
}
回答by user113716
If you really want the text content, and not the value property, then it is possible that you need to trim whitespace.
如果您真的想要文本内容,而不是 value 属性,那么您可能需要修剪 whitespace。
Though in your case, you don't really need the ==
comparison. Your code can be greatly reduced if you just use the text you get back to build the selector.
尽管在您的情况下,您实际上并不需要进行==
比较。如果您只使用返回的文本来构建选择器,则可以大大减少您的代码。
// get (and trim) the text content
var txt = $.trim( $('#target option:selected').text() );
$('#' + txt).show(selectedEffect, options, 500, callback );
回答by saurabh yadav
<script>
$(document).ready(function(){
$('#colorselector').on('change', function() {
if ( this.value == 'red')
{
$("#divid").show();
}
else
{
$("#divid").hide();
}
});
});
</script>
Do like this for every value, Change the values as per your data.
对每个值都这样做,根据您的数据更改值。