我有一段代码:
<form class="myfrom">
<select name="name1"><option>..<option></select>
<select name="name2"><option>..<option></select>
<select name="name3"><option>..<option></select>
</form>
我通过jquery将top block html代码复制到sweetalert。 (所以现在,我在文档中有2个相同的块)...我用这段代码做了:
var form = $('.myfrom').html();
swal({
title: 'edit form',
html: form,
showCancelButton: true
}).then((result) => {
if (result.value) {
swal({
title: 'done',
text: 'it has been done!',
type: 'success',
});
$.ajax({
//call ajax
});
}
如何将selectboxes元素的值传递给then{}
部分?
答案 0 :(得分:0)
您是否尝试过提供您的选择输入ID?
此外,如果你要这样做,你需要在swal({..})代码中创建html,因为jQuery将获取具有指定id的第一个元素。
所以你的代码现在看起来像这样
<强> HTML 强>
<head><!--Incase you don't have jquery loaded-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<!--Not needed anymore-->
<!--<form class="myfrom">
<select name="name1"><option>..<option></select>
<select name="name2"><option>..<option></select>
<select name="name3"><option>..<option></select>
</form>-->
<强>的javascript 强>
swal({
title: 'edit form',
html:'<form class="myfrom">'+
'<select name="name1" id="name1">'+
'<option> .1.</option>' +
'<option> .2.</option>' +
'<option> .3.</option>' +
'</select>'+
'<select name="name2" id="name2">'+
'<option> .1.</option>' +
'<option> .2.</option>' +
'<option> .3.</option>' +
'</select>'+
'<select name="name3" id="name3" > '+
'<option> .1.</option>'+
'<option> .2.</option>'+
'<option> .3.</option>'+
'</select>'+
'</form>',
showCancelButton: true
}).then((result) => {
var name1 = $('#name1').val();//The value of select input name1
var name2 = $('#name2').val();//The value of select input name2
var name3 = $('#name3').val();//The value of select input name3
console.log(name3 + " " + name2 + " " + name3);
if (result.value) {
swal({
title: 'done',
text: 'it has been done!',
type: 'success',
});
$.ajax({
//call ajax
});
}
});
我希望这有助于祝你好运。