我正在使用sweetalert的自定义版本向我的用户询问input
。我已经设法使一切工作,但有一个奇怪的行为,为了能够在输入框中输入文本,你必须先点击屏幕:
swal({
title: "Aggiornamento profilo",
text: '<br /><form method="post" id="taxcode-update" name="taxcodeUpdate"><input id="admin-tax-code" minlength="3" class="form-control wedding-input-text wizard-input-pad" type="text" name="taxCode" placeholder="Codice fiscale"></form>',
type: "warning",
showCancelButton: false,
confirmButtonText: "Aggiorna il mio profilo",
closeOnConfirm: false
}, function () {
swal("Deleted!", "Your imaginary file has been deleted.", "success");
});
swal({
title: "An input!",
text: "Write something interesting:",
type: "input",
showCancelButton: true,
closeOnConfirm: false,
animation: "slide-from-top",
inputPlaceholder: "Write something"
},
function(inputValue){
if (inputValue === false) return false;
if (inputValue === "") {
swal.showInputError("You need to write something!");
return false
}
swal("Nice!", "You wrote: " + inputValue, "success");
});
给input
autofocus
标签。
text: '<br /><form method="post" id="taxcode-update" name="taxcodeUpdate">'
+ '<input id="admin-tax-code" autofocus minlength="3" class="form-control wedding-input-text wizard-input-pad" type="text" name="taxCode" placeholder="Codice fiscale">'
+ '</form>',
试试吧
swal.withForm({
title: 'Cool Swal-Forms example',
text: 'Any text that you consider useful for the form',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Get form data!',
closeOnConfirm: true,
formFields: [
{ id: 'name', placeholder:'Name Field' },
{ id: 'nickname', placeholder:'Add a cool nickname' }
], function(isConfirm) {
// do whatever you want with the form data
console.log(this.swalForm); // { name: 'user name', nickname: 'what the user sends' }
})
使用的变量是'名称'
const {value: name} = await swal({
title: 'Input your name',
input: 'text',
inputPlaceholder: 'Enter here'
})
if (name) {
swal('Entered name: ' + name)
}
使用Sweetalert2。有许多输入提示可以找到here的例子,所有这些都使用现代的异步/等待结构。如果你想要它的旧方法,试试这个:
Swal.fire({
title: "An input!",
text: "Write something interesting:",
input: 'text',
showCancelButton: true
}).then((result) => {
if (result.value) {
console.log("Result: " + result.value);
}
});