问题
I put a multi select list box on MVC form and setup chosen js, I'm able to select options but when i submit the form no values are submitted, when i checked the markup i came to know selected options are not marked as selected, so what expect is below
<option value="1">Test 1</option>
<option value="2" selected>Test 2</option>
<option value="3" selected>Test 3</option>
But what i can see is as in attached picture.
回答1:
Chosen won't add the selected
attribute to your html. If you need to find out what values were selected, use $(selector).val()
.
See below for example. In this example, I am initializing a chosen multiselect and assigning it's selected values to some output to demonstrate $(selector).val()
.
$(document).ready(function() {
$(".chosen-select").chosen();
$(".chosen-select").on("change", function() {
let chosenVal = $(".chosen-select").val();
$("#out").text(chosenVal);
});
});
.chosen-select {
width: 300px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.js"></script>
<select data-placeholder="Choose a Country..." class="chosen-select" multiple="">
<option value=""></option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Aland Islands">Aland Islands</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
</select>
<p id="out"></p>
来源:https://stackoverflow.com/questions/52859012/chosen-js-is-not-marking-options-as-selected