양식에 JavaScript 게시물을 제출하여 새 창을 엽니 다
-
05-07-2019 - |
문제
https://stackoverflow.com/questions/133925/javaScript-post-request-like-aform-submit 게시물을 통해 JavaScript를 통해 작성한 양식을 제출하는 방법을 보여줍니다. 아래는 수정 된 코드입니다.
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "test.jsp");
var hiddenField = document.createElement("input");
hiddenField.setAttribute("name", "id");
hiddenField.setAttribute("value", "bob");
form.appendChild(hiddenField);
document.body.appendChild(form); // Not entirely sure if this is necessary
form.submit();
내가하고 싶은 것은 새로운 창에서 결과를 열는 것입니다. 나는 새로운 창에서 페이지를 열기 위해 이와 같은 것을 사용하고 있습니다.
onclick=window.open(test.html,'','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');
해결책
추가하다
<form target="_blank" ...></form>
또는
form.setAttribute("target", "_blank");
양식의 정의에.
다른 팁
질문에있는대로 JavaScript에서 양식을 작성하고 제출하려면 사용자 정의 기능으로 팝업 창을 만들려면이 솔루션을 제안합니다 (추가 한 줄 위에 의견을 넣었습니다).
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "test.jsp");
// setting form target to a window named 'formresult'
form.setAttribute("target", "formresult");
var hiddenField = document.createElement("input");
hiddenField.setAttribute("name", "id");
hiddenField.setAttribute("value", "bob");
form.appendChild(hiddenField);
document.body.appendChild(form);
// creating the 'formresult' window with custom features prior to submitting the form
window.open('test.html', 'formresult', 'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');
form.submit();
var urlAction = 'whatever.php';
var data = {param1:'value1'};
var $form = $('<form target="_blank" method="POST" action="' + urlAction + '">');
$.each(data, function(k,v){
$form.append('<input type="hidden" name="' + k + '" value="' + v + '">');
});
$form.submit();
이 기본 방법을 알고 있습니다.
1)
<input type=”image” src=”submit.png”> (in any place)
2)
<form name=”print”>
<input type=”hidden” name=”a” value=”<?= $a ?>”>
<input type=”hidden” name=”b” value=”<?= $b ?>”>
<input type=”hidden” name=”c” value=”<?= $c ?>”>
</form>
3)
<script>
$(‘#submit’).click(function(){
open(”,”results”);
with(document.print)
{
method = “POST”;
action = “results.php”;
target = “results”;
submit();
}
});
</script>
공장!
흐름 창에 대한 가장 간단한 작업 솔루션 (Chrome에서 테스트) :
<form action='...' method=post target="result" onsubmit="window.open('','result','width=800,height=400');">
<input name="..">
....
</form>
제휴하지 않습니다 StackOverflow