我正在编写一个读取下拉值然后执行一些操作的代码,我试图弄清楚如何获取选择选项值并为其设置条件。
results['items'][0]
答案 0 :(得分:0)
从下拉菜单中读取值比您更容易:
var select = document.getElementById("selectNewBalance");
// add an event listener to the element
select.addEventListener('change', function(e) {
if (e.target.value === 'Inserted values') {
console.log('You can do something here')
}
})
<select id="selectNewBalance">
<option selected disabled>--</option>
<option value="Inserted values">Inserted values</option>
<option value="Other">Other</option>
</select>
使用jQuery时,代码段如下所示:
jQuery('document').ready(function($) {
$('#selectNewBalance').on('change', function(e) {
if ($(this).val() === 'Inserted values') {
console.log('You can do something here')
}
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="selectNewBalance">
<option selected disabled>--</option>
<option value="Inserted values">Inserted values</option>
<option value="Other">Other</option>
</select>
仅此而已。关键是,您需要将条件置于change
之类的事件中,以便它在输入值更改时运行。