【问题标题】:How to submit a form by clicking a text?如何通过单击文本提交表单?
【发布时间】:2016-11-16 07:50:51
【问题描述】:

我希望在单击span id="ToolBarDownload" 时提交表单,但似乎form id="myformID" 未提交。怎么了?我的代码有意义吗?谢谢!

顺便说一句,id="ToolBarDownload" 的文字在表单之外。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="js/jquery1.10.2.min.js?isassets=1" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#ToolBarDownload").click(function() {
                if (confirm('Do you want to Download')) {
                    $("#myformID").commit();
                }
            });

            $("#myformID").submit(function(eventObj) {
                $('<input />').attr('type', 'hidden').attr('name', "myCw").attr('value', "mydata").appendTo('#myformID');
                return true;
            });
        });
    </script>
</head>
<body>
    <div id="container">
        <form action='' method='post' enctype='multipart/form-data' id="myformID"></form>
        <span id="ToolBarDownload" class="ToolBarButton" style="margin-left:5px">Download</span>
    </div>
</body>
</html>

【问题讨论】:

    标签: jquery html


    【解决方案1】:

    jQuery 中没有 commit() 方法。要提交表单,请使用submit()

    $("#ToolBarDownload").click(function(){
        if (confirm('Do you want to Download')) {
            $("#myformID").submit();
        }
    });
    

    还请注意,在submit() 处理程序中,您可以向attr() 方法提供一个对象,以便只进行一次更改多个属性的调用。 return true 也是多余的,可以去掉:

    $("#myformID").submit(function() {
        $('<input />').attr({
            type: 'hidden',
            name: 'myCw',
            value: 'mydata'
        }).appendTo('#myformID');
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 2017-04-11
    • 1970-01-01
    相关资源
    最近更新 更多