javascript 如何在加载时更改选择框的选定选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27982937/
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
How to change the selected option of a select box onload?
提问by Amrinder Singh
i am working on a project, where i have to select a particular option of a select box, i know how to do this, but the problem is a function must be called whenever value of select box changes, when we do it manually it works, but when i do it with jquery onload, that function not works. Because it only work when a user manually change the option from select box. Please tell me how to do this.
我正在研究一个项目,在那里我必须选择一个选择的一个选择框的选项,我知道如何做到这一点,但问题是在选择框的价值时必须调用一个函数,当我们手动执行它,但是当我使用 jquery onload 执行此操作时,该功能不起作用。因为它仅在用户从选择框中手动更改选项时才有效。请告诉我如何做到这一点。
example:
例子:
<select>
<option val="0" selected></option>
<option val="1"></option>
<option val="2"></option>
<option val="3"></option>
<option val="4"></option>
<option val="5"></option>
</select>
here is some of my efforts which i have tried:
这是我尝试过的一些努力:
$("select").val('5');
回答by Richard Parnaby-King
You wish to pre-select an option when the page first loads?
您希望在页面首次加载时预先选择一个选项吗?
This bit of jQuery will find the option
with the value of 5 and select it. By wrapping it in the jQuery(document).ready
function it will run once the page has loaded.
这一点 jQuery 将找到option
值为 5 的 并选择它。通过将其包装在jQuery(document).ready
函数中,它将在页面加载后运行。
jQuery(document).ready(function($){
$('select').find('option[value=5]').attr('selected','selected');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<select>
<option value="0" selected>0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
回答by Majid Ali Khan
Guess what, it is simple.
你猜怎么着,很简单。
Your Select Component :
您选择的组件:
<select id="YourSelectComponentID">
<option value="0">Apple</option>
<option value="2">Banana</option>
<option value="3">Cat</option>
<option value="4">Dolphin</option>
</select>
Copy and paste this in your javaScript section.
将其复制并粘贴到您的 javaScript 部分。
$(function(){
document.getElementById("YourSelectComponentID").value = 4;
});
Now your option 4 will be selected whenever the page loads.
现在,无论何时加载页面,您的选项 4 都会被选中。
回答by Ganesh Putta
using this you can change the value of select box when page loads
使用它您可以在页面加载时更改选择框的值
jQuery(document).ready(function($){
$('select').find('option[value=USD]').attr('selected','selected');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<select>
<option value="JPY">Japanese yen (JPY)</option>
<option value="AFN">Afghan afghani (AFN)</option>
<option value="ALL">Albanian lek (ALL)</option>
<option value="DZD">Algerian dinar (DZD)</option>
<option value="AOA">Angolan kwanza (AOA)</option>
<option value="ARS">Argentine peso (ARS)</option>
<option value="AMD">Armenian dram (AMD)</option>
<option value="AWG">Aruban florin (AWG)</option>
<option value="USD">United States dollar (USD)</option>
</select>