J'utilise une version personnalisée de sweetalert pour demander à mon utilisateur une input
. J'ai réussi à faire en sorte que tout fonctionne mais il y a un comportement étrange. Pour pouvoir saisir un texte dans la zone de saisie, vous devez d'abord cliquer sur l'écran:
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");
});
Exemple de travail: https://jsfiddle.net/fvkppx06/
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");
});
Donnez à la variable input
la balise 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>',
Juste tester ceci
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' }
})
La variable utilisée est 'name'
const {value: name} = await swal({
title: 'Input your name',
input: 'text',
inputPlaceholder: 'Enter here'
})
if (name) {
swal('Entered name: ' + name)
}