양식 제출 jQuery가 작동하지 않습니다.
나는 그 형태를 가지고있다
<form action="deletprofil.php" id="form_id" method="post">
<div data-role="controlgroup" data-filter="true" data-input="#filterControlgroup-input">
<button type="submit" name="submit" value="1" class="ui-btn ui-shadow ui-corner-all ui-icon-delete ui-btn-icon-right" data-icon="delete" aria-disabled="false">Anlegen</button>
<button type="submit" name="submit" value="2" class="ui-btn ui-shadow ui-corner-all ui-icon-delete ui-btn-icon-right" data-icon="delete" aria-disabled="false">Bnlegen</button>
</div>
</form>
그리고 그 팝업 jQuery Mobile
<div class="ui-popup-container pop in ui-popup-active" id="popupDialog-popup" tabindex="0" style="max-width: 1570px; top: 2239.5px; left: 599px;">
<div data-role="popup" id="popupDialog" data-overlay-theme="b" data-theme="b" data-dismissible="false" style="max-width:400px;" class="ui-popup ui-body-b ui-overlay-shadow ui-corner-all">
<div data-role="header" data-theme="a" role="banner" class="ui-header ui-bar-a">
<h1 class="ui-title" role="heading" aria-level="1">Delete Page?</h1>
</div>
<div role="main" class="ui-content">
<h3 class="ui-title">Sicher dass Sie das Profil löschen wollen?</h3>
<p>Es kann nicht mehr rückgängig gemacht werden.</p>
<a href="#" id="NOlink" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-btn-b">Abbrechen</a>
<a href="#" id="OKlink" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-btn-b">OK</a>
</div>
</div>
</div>
내 jQuery 코드로
<script language="javascript" type="text/javascript">
$(function(){
$('#form_id').bind('submit', function(evt){
$form = this;
evt.preventDefault();
$("#popupDialog").popup('open');
$("#NOlink").bind( "click", function() {
$("#popupDialog").popup('close');
});
$("#OKlink").bind( "click", function() {
$("#popupDialog").popup('close');
$( "#form_id" ).submit();
});
});
});
</script>
팝업이 표시되지만 양식 제출이 작동하지 않습니다. 누군가 아이디어가 있습니까?
NUMBER ONE 오류는 submit
as ID
또는 NAME
양식에 ANYTHING이 있습니다.
양식 요소의 이름을 바꾸십시오 . 그렇지 않으면 .submit()
제출 메소드 / 핸들러가 name / id 속성에 의해 가려지기 때문에 양식을 호출 할 수 없습니다 .
다른 몇 가지 :
언급했듯이 jQuery보다 간단한 이벤트를 사용하여 양식을 제출해야합니다.
하지만 링크 클릭을 취소해야합니다.
그런데 왜 두 개의 버튼이 있습니까? jQuery를 사용하여 양식을 제출하기 때문에 클릭시 숨겨진 필드를 설정하지 않는 한 두 버튼 중 어떤 버튼이 클릭되었는지 알 수 없습니다.
<form action="deletprofil.php" id="form_id" method="post">
<div data-role="controlgroup" data-filter="true" data-input="#filterControlgroup-input">
<button type="submit" value="1" class="ui-btn ui-shadow ui-corner-all ui-icon-delete ui-btn-icon-right" data-icon="delete" aria-disabled="false">Anlegen</button>
<button type="submit" value="2" class="ui-btn ui-shadow ui-corner-all ui-icon-delete ui-btn-icon-right" data-icon="delete" aria-disabled="false">Bnlegen</button>
</div>
</form>
$(function(){
$("#NOlink, #OKlink").on("click", function(e) {
e.preventDefault(); // cancel default action
$("#popupDialog").popup('close');
if (this.id=="OKlink") {
document.getElementById("form_id").submit(); // or $("#form_id")[0].submit();
}
});
$('#form_id').on('submit', function(e){
e.preventDefault();
$("#popupDialog").popup('open');
});
});
귀하의 의견을 고려할 때 정말로 이것을하고 싶다고 생각합니다.
<form action="deletprofil.php" id="form_id" method="post">
<input type="hidden" id="whichdelete" name="whichdelete" value="" />
<div data-role="controlgroup" data-filter="true" data-input="#filterControlgroup-input">
<button type="button" value="1" class="delete ui-btn ui-shadow ui-corner-all ui-icon-delete ui-btn-icon-right" data-icon="delete" aria-disabled="false">Anlegen</button>
<button type="button" value="2" class="delete ui-btn ui-shadow ui-corner-all ui-icon-delete ui-btn-icon-right" data-icon="delete" aria-disabled="false">Bnlegen</button>
</div>
</form>
$(function(){
$("#NOlink, #OKlink").on("click", function(e) {
e.preventDefault(); // cancel default action
$("#popupDialog").popup('close');
if (this.id=="OKlink") {
// trigger the submit event, not the event handler
document.getElementById("form_id").submit(); // or $("#form_id")[0].submit();
}
});
$(".delete").on("click", function(e) {
$("#whichdelete").val(this.value);
});
$('#form_id').on('submit', function(e){
e.preventDefault();
$("#popupDialog").popup('open');
});
});
Because when you call $( "#form_id" ).submit();
it triggers the external submit handler which prevents the default action, instead use
$( "#form_id" )[0].submit();
or
$form.submit();//declare `$form as a local variable by using var $form = this;
When you call the dom element's submit method programatically, it won't trigger the submit handlers attached to the element
According to http://api.jquery.com/submit/
The submit event is sent to an element when the user is attempting to submit a form. It can only be attached to 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.
So basically, .submit
is a binding function, to submit the form you can use simple Javascript:
document.formName.submit().
if there is one error in the the submit function,the submit function will be execute. in other sentences prevent default(or return false) does not work when one error exist in submit function.
If you are doing form validation such as
type="submit" onsubmit="return validateForm(this)"
validateForm = function(form) {
if ($('input#company').val() === "" || $('input#company').val() === "Company") {
$('input#company').val("Company").css('color','red'); finalReturn = false;
$('input#company').on('mouseover',(function() {
$('input#company').val("").css('color','black');
$('input#company').off('mouseover');
finalReturn = true;
}));
}
return finalReturn;
}
Double check you are returning true. This seems simple but I had
var finalReturn = false;
When the form was correct it was not being corrected by validateForm and so not being submitted as finalReturn was still initialized to false instead of true. By the way, above code works nicely with address, city, state and so on.
Alright, this doesn't apply to the OP's exact situation, but for anyone like myself who comes here facing a similar issue, figure I should throw this out there-- maybe save a headache or two.
If you're using an non-standard "button" to ensure the submit
event isn't called:
<form>
<input type="hidden" name="hide" value="1">
<a href="#" onclick="submitWithChecked(this.form)">Hide Selected</a>
</form>
Then, when you try to access this.form
in the script, it's going to come up undefined.
Instead, you can use a button
with type="button"
<form>
<input type="hidden" name="hide" value="1">
<button type="button" onclick="submitWithChecked(this.form)">Hide Selected</a>
</form>
(Haven't tested this across browsers yet, so someone might need to call me out on that.)
Don't forget to close your form with a </form>
. That stopped submit() working for me.
Since every control element gets referenced with its name on the form element (see forms specs), controls with name "submit" will override the build-in submit function.
Which leads to the error mentioned in comments above:
Uncaught TypeError: Property 'submit' of object
#<HTMLFormElement>
is not a function
As in the accepted answer above the simplest solution would be to change the name of that control element.
However another solution could be to use dispatchEvent
method on form element:
$("#form_id")[0].dispatchEvent(new Event('submit'));
Some time you have to give all the form element into a same div.
example:-
If you are using ajax submit with modal.
So all the elements are in modal body.
Some time we put submit button in modal footer.
ReferenceURL : https://stackoverflow.com/questions/22982741/form-submit-jquery-does-not-work
'programing' 카테고리의 다른 글
LESS에서 if 문을 사용하는 방법 (0) | 2021.01.16 |
---|---|
여러 구분 기호로 문자열 분할 (0) | 2021.01.16 |
pip.req라는 모듈이 없습니다. (0) | 2021.01.16 |
사용자가 모바일 Safari에서 탐색했는지 확인 (0) | 2021.01.16 |
UITableViewCell 클릭시 확장 (0) | 2021.01.16 |