[英]how to change select box selected value? which means i want to show the selected value, rather than the text?
i want to display the selected value, rather than the text. 我想显示选定的值,而不是文本。 like
喜欢
instead 代替
i have tried with below code, but doesn't works for me, any help would be appreciated 我已经尝试过下面的代码,但对我不起作用,任何帮助将不胜感激
<select class="form-control" name="identity" id="identity"> <option value="CC">Cédula de Ciudadanía</option> <option value="CE">Cédula Extranjera</option> <option value="NIT">Nit</option> <option value="TI">Tarjeta de Identidad</option> <option value="PPN">Pasaporte</option> <option value="SSN">Número de Seguridad Social</option> <option value="LIC">Licencia de Conducción</option> <option value="DNI">DNI</option> </select>
but it sholud not reflect the select box values 但它不能反映选择框的值
div
element over select
element select
元素上有一个伪div
元素 pointer-events: none;
pointer-events: none;
for fake
element fake
元素 change
event of select-element
select-element
change
事件 text
of the fake
element on change of the select-element
select-element
更改时设置fake
元素的text
var $identity = $('#identity'); var $fake = $('#fake'); $fake.css($identity[0].getBoundingClientRect()); $identity.on('change', function() { $fake.text($(this).find('option:selected').text()); }).change();
#fake { pointer-events: none; border: 1px solid black; padding: 3px; } #identity { opacity: 0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <select class="form-control" name="identity" id="identity"> <option value="CC">Cédula de Ciudadanía</option> <option value="CE">Cédula Extranjera</option> <option value="NIT">Nit</option> <option value="TI">Tarjeta de Identidad</option> <option value="PPN">Pasaporte</option> <option value="SSN">Número de Seguridad Social</option> <option value="LIC">Licencia de Conducción</option> <option value="DNI">DNI</option> </select> <div id="fake" style="position:absolute;"></div>
Sadly, this is not possible with the native HTML <select>
. 遗憾的是,使用本机HTML
<select>
不可能做到这一点。 But, you can look for other <select>
libraries which can do this or write your own <select>
component which does this. 但是,您可以寻找其他可以执行此操作的
<select>
库,也可以编写自己的<select>
组件来执行此操作。
var $identity = $('#identity'); var $fake = $('#fake'); $fake.css($identity[0].getBoundingClientRect()); $identity.on('change', function() { $fake.text($(this).find('option:selected').val()); }).change();
#fake { pointer-events: none; border: 1px solid black; padding: 0px; height: 32px !important; line-height: 32px; } #identity { opacity: 0; height: 32px !important; line-height: 32px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <select class="form-control" name="identity" id="identity"> <option value="CC">Cédula de Ciudadanía</option> <option value="CE">Cédula Extranjera</option> <option value="NIT">Nit</option> <option value="TI">Tarjeta de Identidad</option> <option value="PPN">Pasaporte</option> <option value="SSN">Número de Seguridad Social</option> <option value="LIC">Licencia de Conducción</option> <option value="DNI">DNI</option> </select> <div id="fake" style="position:absolute;"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.