Определить действие после публикации в форме - PullRequest
0 голосов
/ 27 ноября 2011

Я хочу что-то сделать при отправке формы.

var ispostaction = false;
$("#myform").submit(function () {
 ispostaction = true;
});

При отправке формы .submit (function ()) не вызывается. Что-то не так, что я делаю? У меня есть идентификатор формы как моя форма

Буду признателен за любую помощь.

вот моя страница xhtml. Я использую JSF 2

<form id="myform" class="someclass" enctype="application/x-www-form-urlencoded"
action="pagename.jsf" method="post">
// custom input text field
// selectOneMenu
// a few more input text fields 
// submit button is below
<input id="javax.faces.ViewState" type="hidden" autocomplete="off" value="...." name="javax.faces.ViewState">
</form>

1 Ответ

2 голосов
/ 28 ноября 2011

Документация jquery:

The submit event is sent to an element when the user is attempting to submit
a form. It can only be attached to <form> elements. Forms can be submitted
either by clicking an explicit <input type="submit">, <input type="image">,
or <button type="submit">, or by pressing Enter when certain form elements
have focus.

Вызов функции отправки не вызовет событие отправки. Вы можете «исправить» это, добавив скрытую кнопку, которую вы нажимаете вместо jquery. Большинство, если не все, браузеры, к сожалению, ведут себя одинаково.

<html>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<body>
<form id="myform" enctype="application/x-www-form-urlencoded"
action="posturl" method="post">
// custom input text field
// selectOneMenu
// a few more input text fields 
// submit button is below
<input id="javax.faces.ViewState" type="hidden" autocomplete="off" value="...." name="javax.faces.ViewState">
<input type="text" value="a value" />
<input id="submit" type="submit" value="submit" style="display: none;" />
</form>

<script type="text/javascript">
$(document).ready(function() {
    $("#myform").bind('submit', function() {
        alert('');
    });
    $("#submit").click();
});
</script>

</body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...