[英]How can I obtain the selected option into a select dropodown tag using JavaScript?
I am pretty new in JavaScript and I have the following problem. 我在JavaScript中还很陌生,但遇到以下问题。
Into a page I have the following select dropdown: 进入页面后,我有以下选择下拉列表:
<select id="selTipoRap" class="form-control" th:field="*{tipoRappresentante}" required="required" style="width: 55%;" onChange="nascondiBoxDocumentazione(this);">
......................................................................
......................................................................
OPTIONS LIST
......................................................................
......................................................................
</select>
Then I have this JavaScript function that is performed when the usere select an option into the previous select: 然后,当用户在上一个选择中选择一个选项时,我将执行此JavaScript函数:
function nascondiBoxDocumentazione(ruoloSelezionato) {
alert("NASCONDI");
}
How can I obtain the value of the selected option into this JavaScript function? 如何获得此JavaScript函数中所选选项的值?
function nascondiBoxDocumentazione(ruoloSelezionato) { alert(ruoloSelezionato.value); }
<select id="selTipoRap" class="form-control" style="width: 55%;" onChange="nascondiBoxDocumentazione(this);"> <option>Option 1</option> <option>Option 2</option> </select>
I think you want something like this 我想你想要这样的东西
EDIT: Following a comment from Alexander Solonik, my previous answer was not optimal, this should be much more efficient. 编辑:根据亚历山大·索洛尼克(Alexander Solonik)的评论,我以前的回答不是最佳的,这应该效率更高。
$('#selTipoRap').on('change', function() { alert($(this).val()); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="selTipoRap"> <option value="0">Zero</option> <option value="1 ">One</option> <option value="2 ">Two</option> <option value="3 ">Three</option> </select>
A pure JavaScript solution: 纯JavaScript解决方案:
var selTipoRap = document.getElementById('selTipoRap'); selTipoRap.onchange = function() { alert(selTipoRap.value) }
<select id="selTipoRap"> <option value="0">Zero</option> <option value="1 ">One</option> <option value="2 ">Two</option> <option value="3 ">Three</option> </select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.