我试图获取所选元素的值,将它们添加到一个数组中并发送。 但我的代码不起作用。
<link rel="stylesheet" href="https://res.cloudinary.com/dxfq3iotg/raw/upload/v1569006288/BBBootstrap/choices.min.css?version=7.0.0">
<script src="https://res.cloudinary.com/dxfq3iotg/raw/upload/v1569006273/BBBootstrap/choices.min.js?version=7.0.0"></script>
<div class="row d-flex justify-content-center mt-100">
<div class="col-md-6">
<select id="choices-multiple-remove-button" multiple>
<option value="Acupuncturist" /> Acupuncturist</option>
<option value="Holistic Care" /> Holistic Care</option>
<option value="Naturopathic Doctor" /> Naturopathic Doctor</option>
</select>
</div>
</div>
$(document).ready(function() {
var multipleCancelButton = new Choices('#choices-multiple-remove-button', {
removeItemButton: true,
});
});
$('#scrape').on('click', () => {
var brands = $('#choices-multiple-remove-button option:selected');
var specialty = [];
$(brands).each(function(index, brand) {
specialty.push([$(this).val()]);
});
console.log(specialty);
$.post('/wellness', { 'specialty': specialty }, (res) => {});
});
我按照Andreas和Mosh Feu的评论编写了以下片段。 也许你能解释一下,什么东西在这里不起作用?
(我注释掉了$.post()
部分,因为这在SO片段中不起作用。)
null
$(document).ready(function() {
var multipleCancelButton = new Choices('#choices-multiple-remove-button', {
removeItemButton: true,
});
});
$('#scrape').on('click', () => {
var brands = $('#choices-multiple-remove-button option:selected');
var specialty = [];
brands.each(function(index, brand) {
specialty.push($(this).val());
});
console.log(specialty);
// $.post('/wellness', { 'specialty': specialty }, (res) => {});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://res.cloudinary.com/dxfq3iotg/raw/upload/v1569006288/BBBootstrap/choices.min.css?version=7.0.0">
<script src="https://res.cloudinary.com/dxfq3iotg/raw/upload/v1569006273/BBBootstrap/choices.min.js?version=7.0.0"></script>
<div class="row d-flex justify-content-center mt-100">
<div class="col-md-6">
<select id="choices-multiple-remove-button" multiple>
<option value="Acupuncturist" /> Acupuncturist</option>
<option value="Holistic Care" /> Holistic Care</option>
<option value="Naturopathic Doctor" /> Naturopathic Doctor</option>
</select>
</div>
<button id="scrape">scrape</button>
</div>