Dialoghi di modulo modale Bootstrap 2 Twitter Bootstrap 2
-
27-10-2019 - |
Domanda
Ho il seguente modulo di dialogo:
<div class='modal' id='myModal'>
<div class='modal-header'>
<a class='close' data-dismiss='modal'>×</a>
<h3>Add Tags</h3>
</div>
<div class='modal-body'>
<form accept-charset="UTF-8" action="/tagging" data-remote="true" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="mCNvbvoPFWhD7SoJm9FPDh+BcRvCG3d16P+oOFACPuc=" /></div>
<input id="tags_string" name="tags_string" type="text" value="luca" />
<input id="id" name="id" type="hidden" value="4f1c95fd1d41c80ff200067f" />
</form>
</div>
<div class='modal-footer'>
<div class='btn btn-primary'><input name="commit" type="submit" value="Add tag" /></div>
</div>
</div>
e il suo js:
<script>
//<![CDATA[
$(function() {
// wire up the buttons to dismiss the modal when shown
$("#myModal").bind("show", function() {
$("#myModal a.btn").click(function(e) {
// do something based on which button was clicked
// we just log the contents of the link element for demo purposes
console.log("button pressed: "+$(this).html());
// hide the dialog box
$("#myModal").modal('hide');
});
});
// remove the event listeners when the dialog is hidden
$("#myModal").bind("hide", function() {
// remove event listeners on the buttons
$("#myModal a.btn").unbind();
});
// finally, wire up the actual modal functionality and show the dialog
$("#myModal").modal({
"backdrop" : "static",
"keyboard" : true,
"show" : true // this parameter ensures the modal is shown immediately
});
});
//]]>
</script>
Quando faccio clic su X, che è <a class='close' data-dismiss='modal'>×</a>
, il modulo si chiude a lasciarmi sulla pagina corrente, mentre mi piacerebbe andare su HamePage.
Anche "Aggiungi tag", che è <div class='btn btn-primary'><input name="commit" type="submit" value="Add tag" /></div>
Non fare nulla, mentre fai clic su Jaust Inserisci sulla tastiera, fai il lavoro e vorrei fare clic su "Aggiungi tag" ha fatto lo stesso.
Non sono così abile su JS e Front-End Prog, quindi qualsiasi aiuto è il benvenuto.
Soluzione
Il pulsante di invio è al di fuori dei tag del modulo.
Non saprà quale modulo inviare.
Usa JavaScript per collegarlo al modulo.
<div class='modal-body'>
<form id="modal-form" accept-charset="UTF-8" action="/tagging" data-remote="true" method="post">
<input name="something" value="Some value" />
</form>
</div>
<div class='modal-footer'>
<a id="modal-form-submit" class='btn btn-primary' href="#">Submit</a>
</div>
<script>
$('#modal-form-submit').on('click', function(e){
// We don't want this to act as a link so cancel the link action
e.preventDefault();
// Find form and submit it
$('#modal-form').submit();
});
</script>
Per quanto riguarda la <a class='close' data-dismiss='modal'>×</a>
Ciò dovrebbe collegarsi alla homepage, perché non rimuovere semplicemente il data-dismiss='modal'
e farlo agire come un link normale usando uno standard href='home.html'
.
Ecco un codice aggiuntivo per indicarti nella giusta direzione per l'utilizzo di Ajax per inviare il modulo:
// Since we want both pressing 'Enter' and clicking the button to work
// We'll subscribe to the submit event, which is triggered by both
$('#modal-form').on('submit', function(){
//Serialize the form and post it to the server
$.post("/yourReceivingPage", $(this).serialize(), function(){
// When this executes, we know the form was submitted
// To give some time for the animation,
// let's add a delay of 200 ms before the redirect
var delay = 200;
setTimeout(function(){
window.location.href = 'successUrl.html';
}, delay);
// Hide the modal
$("#my-modal").modal('hide');
});
// Stop the normal form submission
return false;
});
Altri suggerimenti
Prendere il Pulsante Invia Il lavoro lo mette nella forma.
<div class="modal">
<form id="modal-form" action="/tagging" data-remote="true" method="post">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>A Modal Form</h3>
</div>
<div class="modal-body">
<input name="something" value="Some value" />
</div>
<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">Cancel</a>
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
Tuttavia, questo aggiunge un margine inaspettato nella parte inferiore del modale. Bootstrap 2.0.2 ha introdotto il modal-form
classe Per sistemare questo o puoi risolverlo da solo con una definizione di stile come:
.modal > form {
margin-bottom: 0;
}
Per Collegamento a un'altra pagina quando si chiude il modale Vado con teshellfishmeme
Per quanto riguarda il × che dovrebbe collegarsi alla homepage, perché non rimuovere semplicemente i dati-Dismiss = 'modal' e farlo agire come un link normale usando un href = 'home.html' standard.
Con HTML5 puoi avere qualcosa di simile:
<div class="modal" id="myModal">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Add Tags</h3>
</div>
<div class="modal-body">
<form id="my_form" accept-charset="UTF-8" action="/tagging" data-remote="true" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓" />
<input name="authenticity_token" type="hidden" value="mCNvbvoPFWhD7SoJm9FPDh+BcRvCG3d16P+oOFACPuc=" />
</div>
<input id="tags_string" name="tags_string" type="text" value="luca" />
<input id="id" name="id" type="hidden" value="4f1c95fd1d41c80ff200067f" />
</form>
</div>
<div class="modal-footer">
<div class="btn btn-primary"><input name="commit" type="submit" value="Add tag" form="my_form" /></div>
</div>
</div>
Questo ha chiamato in html5 Elemento associato alla forma OF-CORSE Se devi supportare tutti i browser + quelli vecchi, allora devi andare con JavaScript, ma puoi usare JavaScript come fallback :)
Il problema per la presentazione del modulo si trova all'interno della BOOTSTRAP JS Modal Library (bootstrap-modal.js) - Basicaly Invio è stato impedito a causa della linea #204: ev.preventDefault
(perché?).
La mia soluzione era aggiungere:
if(!$(e.target).parents('form'))
e.preventDefault();
Tuttavia non so quali problemi si genereranno.
FYI puoi fare quanto segue (scritto in giada):
.modal.hide.fadel
form.modal-form
.modal-header
button.close type='button' data-dismiss="modal" x
h3= t 'translation'
.modal-body
p hello
.modal-footer
button.btn data-dismiss="modal" href="#" = t 'close'
a.btn.btn-primary data-dismiss="modal" data-remote="true" href="#"= t 'form.submit'