我正在尝试使用jQuery将表单加载中的下拉值默认为默认值。
当我使用此$("#FORM_COMPANY").val("All");
时,下拉值是默认值,但是在声明变量时,如下所示:
var defaultValue= "somevalue";
$("#FORM_COMPANY").val(defaultValue);
然后下拉菜单未默认设置,有谁可以帮忙
答案 0 :(得分:2)
$(document).ready(function(){
var defaultValue= "All";
$("#FORM_COMPANY").val(defaultValue);
});
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
</head>
<body>
<select id="FORM_COMPANY">
<option value="">Select Value</option>
<option value="All">All</option>
<option value="other">other</option>
</select>
</body>
</html>
答案 1 :(得分:0)
很难说100%肯定没有看到您的<select>
和<option>
列表,但是仅基于问题的详细信息,我可以认为您正在尝试分配默认值包含在<option value="???">
$=jQuery;
var firstDefaultValue = 'first';
$('#FORM_COMPANY1').val(firstDefaultValue);
// Will work, because inside your <select> there is an option with value="first"
var secondDefaultValue = 'IamNotListedInAnyOption';
$('#FORM_COMPANY2').val(secondDefaultValue);
// Will NOT work, because inside your <select> there is NO option with value="IamNotListedInAnyOption"
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
</head>
<body>
<select id="FORM_COMPANY1">
<option value="default">Select Value</option>
<option value="first">First</option>
<option value="second">Second</option>
</select>
<select id="FORM_COMPANY2">
<option value="default">Select Value</option>
<option value="first">First</option>
<option value="second">Second</option>
</select>
</body>
</html>