Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试使用jquery形成提交,不会';行不通_Javascript_Jquery_Dom - Fatal编程技术网

Javascript 尝试使用jquery形成提交,不会';行不通

Javascript 尝试使用jquery形成提交,不会';行不通,javascript,jquery,dom,Javascript,Jquery,Dom,我正在尝试使用jquery将表单数据发布到新窗口。但我失败了 我的源代码是这样的 <!doctype html> <html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.0.min.js" > </script> <script type="text/javascript"> functi

我正在尝试使用jquery将表单数据发布到新窗口。但我失败了

我的源代码是这样的

<!doctype html>
<html>
 <head>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.0.min.js" > </script>
  <script type="text/javascript">
        function submit_with_jquery(){
     var $form = $("<form></form>");
     var $input = $("<input name='imgInput' />");
     $form.prop("target", "photo_top")
        .prop("method", "post")
        .prop("action", "/common/board/photoViewWindow.jsp")
        .append($input)
        .bind("submit", 
            function(){ 
                window.open('','photo_pop','width=720,height=530,toolbar=no,resizable=no,scrollbars=no,left=50,top=50'); 
            } )
        .submit();
    }
  </script>
 </head>
 <body>
        <input type="button" onclick="submit_with_jquery()" value="submit with jquery" />
 </body>
</html>
我认为它的工作原理是一样的,但jQuery代码不是。
为什么我的第一个代码不起作用?我哪里出错了?

您的
目标是
“photo\u top”
,您打开的窗口名为
“photo\u pop”


除此之外,与第一种方法相比,Ajax可能是一个更好的选择,在这种方法中,手动构建表单元素只是为了提交它们。

特别是对于最新版本的jQuery,您应该使用
.prop()
而不是
.attr()
@Pointy我不知道这一点。非常感谢@尖刻:你认为这真的与
目标
方法
操作
属性相关吗?我不认为这些处理方式是不同的。@Bergi我也有点困惑,但1.9似乎更清楚地表明设置属性和设置属性是两件完全不同的事情。对于这样一个新创建的元素,我不太确定它到底是什么,但就我个人而言,我将使用
.prop()
在所有情况下,如果没有jQuery,我将直接使用DOM节点属性。target、method和action都在表单标记的属性集中,因此使用.attr()完全可以。这不是问题,是我的错。非常感谢。
<!doctype html>
<html>
 <head>
  <script type="text/javascript">
    function submit_with_tag(){
     window.open('','photo_pop','width=720,height=530,toolbar=no,resizable=no,scrollbars=no,left=50,top=50');
    }      </script>
 </head>
 <body>
    <form id="tagForm" action="/common/board/photoViewWindow.jsp" target="photo_pop" onSubmit="submit_with_tag()">
        <input name="imgInput" type="hidden"/>
        <input type="submit" value="submit with tag"/>
    </form>
     </body>
</html>