Code:
<tr>
<th>空间性质</th>
<td>
<input type="hidden" id = "class" value="{$post.post_class}"/>
<select class="form-control" name="post[post_class]" id="class2" value="{$post.post_class}">
<option value="0" id="op1">出售</option>
<option value="1" id="op2">出租</option>
</select>
</td>
</tr>
Display different option values according to the value of value={$post.post_class}
, value only has two values 0 and 1. TKS
黄舟2017-06-28 09:27:35
The default selection is right, just use jquery's attr. Assuming that the default selection value is 1, the code is as follows:
$("#class option[value='1']").attr('selected',true);
我想大声告诉你2017-06-28 09:27:35
$("#class option[value='1']").attr('selected',true);
或
$("#class").val(1);
给我你的怀抱2017-06-28 09:27:35
Due to: document.querySelector('#class').value
The value in select cannot be obtained (i.e. <select class="form-control" name="post[post_class]" id="class2" value="{$post.post_class}">
).
So add a hidden input <input type="hidden" id = "class" value="{$post.post_class}"/>
to get the value sent from the background, and then judge.
<script type="text/javascript">
var sv = document.getElementById('class').value;
if(sv == 0){
$("#class2 option[value='0']").attr('selected',true);
}else {
$("#class2 option[value='1']").attr('selected',true);
}
</script>