我试图使用javascript函数提交表单,下面是我的javascript函数
function submit_form(id_textarea,image_name_IN){
//var value_filled_textbox=document.getElementById(textarea_id_in).value;
var form=document.createElement("form_temp");//temporary dummy form
form.setAttribute("method","post");//its a post method
form.setAttribute("action","comment.php"); // to which the page needs to be submitted
var comment_value=document.getElementById(id_textarea).value; // gets the content of textarea
// alert(comment_value);// just to make sure that you are getting the commeted text area content
//create a hidden field for comments text
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "comment_content");
hiddenField.setAttribute("value", comment_value);
form.appendChild(hiddenField);
// create a hidden field for passing image name
var hiddenField2 = document.createElement("input");
hiddenField2.setAttribute("type", "hidden");
hiddenField2.setAttribute("name", "image_name" );
hiddenField2.setAttribute("value",image_name_IN );
form.appendChild(hiddenField2);
document.body.appendChild(form);
form.submit();
//alert(value_filled_textbox);
window.open("http://127.0.0.1/comment.php");
//document.getElementById('myform').submit();
}
发表评论时, 我收到两个comment.php窗口,一个有结果,另一个有两个警告说未定义值“image_name”和“comment_content”
答案 0 :(得分:2)
这是因为form_temp不是有效的表单元素。创建一个只是一个表单元素,它将获得提交函数
var form=document.createElement("form");//temporary dummy form
答案 1 :(得分:2)
您的表单必须是<form>
...您使用的是不存在的HTML元素<form_temp>
,其中(当然)没有submit()方法
var form=document.createElement("form");//temporary dummy form
答案 2 :(得分:0)
USE
var comment_value = document.getElementById('id_textarea')。value;
而不是
var comment_value = document.getElementById(id_textarea).value;