知道value值。怎么设置默认选中
document.getElementById('select的id').value='值'
<option value="xxx" selected>xxx</option>
<!DOCTYPE HTML> <html lang="en"> <head> <title>Mighty Gumball (Json)</title> <meta charset="utf-8"> </head> <body> <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0-beta1.js"></script> <select id="select"> <option value="a">a</option> <option value="b">b</option> <option value="c">c</option> <option value="d">d</option> <option value="e">e</option> </select> <script type = "text/javascript"> $(function(){ var ck = 'd'; $("select option").each(function(){ console.log($(this).val()) if($(this).val() == ck){ $(this).prop('selected',true); } }) }) </script> </body> </html>
window.onload=function(){ var title = document.getElementById('testid'); for(var i=0;i<title.options.length;i++) { if(title.options[i].value == 'test'){ title.options[i].selected = true; break; } } }
或者直接 jquery $("select").val('选中值');