使用http://en.allexperts.com/q/Javascript-1520/create-form-submit-fly.htm作为参考,我创建了以下表单,它可以通过onclick事件或任何其他用户事件提交?有没有办法在页面加载时执行此操作,或者我是否必须始终等待用户事件?
<script>
var params = something
//helper function to create the form
function getNewSubmitForm(){
var submitForm = document.createElement("FORM");
document.body.appendChild(submitForm);
submitForm.method = "POST";
return submitForm;
}
//helper function to add elements to the form
function createNewFormElement(inputForm, elementName, elementValue){
var newElement = document.createElement("<input name='"+elementName+"' type='hidden'>");
inputForm.appendChild(newElement);
newElement.value = elementValue;
return newElement;
}
function createFormAndSubmit(){
var submitForm = getNewSubmitForm();
createNewFormElement(submitForm, "field1", params.data1);
createNewFormElement(submitForm, "field2", params.data2);
submitForm.name= "submitpost";
submitForm.action= "some URL";
submitForm.submit();
}
</script>
答案 0 :(得分:0)
如果我理解正确,你想做类似以下的事情。正确?
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var params = ["1","7"];
createFormAndSubmit();
//helper function to create the form
function getNewSubmitForm(){
var submitForm = document.createElement("FORM");
document.body.appendChild(submitForm);
submitForm.method = "POST";
return submitForm;
}
//helper function to add elements to the form
function createNewFormElement(inputForm, elementName, elementValue){
var newElement = document.createElement("INPUT");
newElement.name = elementName;
newElement.type = 'hidden';
newElement.value = elementValue;
inputForm.appendChild(newElement);
return newElement;
}
function createFormAndSubmit(){
var submitForm = getNewSubmitForm();
createNewFormElement(submitForm, "field1", params[0]);
createNewFormElement(submitForm, "field2", params[1]);
submitForm.name= "submitpost";
submitForm.action= "show.jsp";
submitForm.submit();
}
});
</script>