admin管理员组文章数量:1399260
I want to create a dropdown list, which should display value instead of text.
But, when I click the dropdown, the option should display the text instead of value.
I have added an example here, but it is not working as expected:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<script src=".2.1/jquery.min.js"></script>
</head>
<body>
<select>
<option name="One" value="1">One</option>
<option name="Two" value="2">Two</option>
<option name="Three" value="3">Three</option>
<option name="Four" value="4">Four</option>
</select>
<script>
$(document).ready(function() {
$('select').click(function() {
$('select :selected').text($('select :selected').val());
});
});
</script>
</body>
</html>
I want to create a dropdown list, which should display value instead of text.
But, when I click the dropdown, the option should display the text instead of value.
I have added an example here, but it is not working as expected:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<select>
<option name="One" value="1">One</option>
<option name="Two" value="2">Two</option>
<option name="Three" value="3">Three</option>
<option name="Four" value="4">Four</option>
</select>
<script>
$(document).ready(function() {
$('select').click(function() {
$('select :selected').text($('select :selected').val());
});
});
</script>
</body>
</html>
This is a picture of the example.
Share Improve this question edited Jul 27, 2017 at 10:16 Raffel asked Jul 26, 2017 at 11:18 RaffelRaffel 711 silver badge10 bronze badges 8-
1
it should be
$(this).find('option:selected').val()
– treyBake Commented Jul 26, 2017 at 11:20 - It should be mousedown, not click, but there's no way you'll have "1" and "one" at the same time with the standard widget. You need to write your own widget or hide part of the standard one (for example with a div) – Denys Séguret Commented Jul 26, 2017 at 11:22
-
It isn't possible to create something as per your picture using a native
<select>
. You'd need a plugin that uses CSS/Script to emulate a dropdown effect. Maybe just put "1 (One)" as the text in the first place? Also<option>
tags don't have a "name" attribute. That belongs on the<select>
and is used as the name of the field when submitting the form to the server. developer.mozilla/en-US/docs/Web/HTML/Element/option – ADyson Commented Jul 26, 2017 at 11:23 - @ThisGuyHasTwoThumbs thank you for your correction. – Raffel Commented Jul 26, 2017 at 11:26
- @Raffel no worries :) – treyBake Commented Jul 26, 2017 at 11:27
3 Answers
Reset to default 3Try this
$(document).ready(function() {
$("select option[value="+$('select option:selected').val()+"]").css({"background-color":"#0000ff","color":"#fff"});
$('select').change(function() {
$('select option')[0].value=$('select option:selected').val();
$('select option')[0].innerHTML=$('select option:selected').val();
$("select").val($('select option:selected').val());
$("select option").css({"background-color":"","color":""});
$("select option[value="+$('select option:selected').val()+"]").css({"background-color":"#0000ff","color":"#fff"});
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option name="One" value="1" style="display:none">1</option>
<option name="One" value="1">One</option>
<option name="Two" value="2">Two</option>
<option name="Three" value="3">Three</option>
<option name="Four" value="4">Four</option>
</select>
I have created an extra option which will be display none. Now whenever user will change any option I will change the value and text of the 1st element which is not displaying and Then setting the first value to be selected
If you are using bootstrap, I suggest using dropdown-menu and add a click listener on each dropdown item to change the dropdown-menu text.
this is a sample code I use for filtering table and show icon of filter type instead of value text.
<div class="btn-group flex-fill" dir="ltr">
<input type="text" enterkeyhint="search" class="form-control form-control-sm" id="filter_title" autoplete="off" onclick="$(this).val('')">
<button class="btn border dropdown-toggle p-1" data-toggle="dropdown">
<span id="ifilter_title"></span>
</button>
<div class="dropdown-menu text-center">
<a class="dropdown-item" onclick="$('#ifilter_title').text('1');"><span>One</span></a>
<a class="dropdown-item" onclick="$('#ifilter_title').text('2');"><span>Two</span></a>
<a class="dropdown-item" onclick="$('#ifilter_title').text('3');"><span>Three</span></a>
<a class="dropdown-item" onclick="$('#ifilter_title').text('4');"><span>Four</span></a>
</div>
It's impossible to do for a native select element, try adding label property in your option, <option value="1" label="1" selected>One</option>
本文标签: javascriptDisplay value in select tag instead of textStack Overflow
版权声明:本文标题:javascript - Display value in select tag instead of text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744106573a2591090.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论