繁体   English   中英

jQuery通过Submit按钮提交表单

[英]jQuery submit form via submit button

是否可以通过单击div元素来提交表单,就像通过“提交”按钮提交表单一样?

为了使该PHP正常工作:

if(isset($_POST["share"])) { /* do something*/ }

形成:

<form id="form" action="publish.php" method="POST">

  <textarea name="description" maxlength="500">Description...</textarea>

  <input type="submit" name="share" value="Share" />

</form>

这不会发布份额值$ _POST ['share']。

if($(".post-form").length){

        $(".post-form").click(function(event) {

            $("#form").submit();
            return false;

        });

    }

是的,这可以通过使用.submit()函数来实现。 您可以这样使用它:

// Wait until the document has been fully loaded
$(document).ready(function() {

    // Bind a click event to your element
    $('div').click(function(event) {

        // Submit the form
        // The callback should add a hidden field with the name "share"
        $('form').submit(function(eventObj) {
            $('<input />').attr('type', 'hidden')
                .attr('name', "share")
                .attr('value', "Share")
                .appendTo('form');
            return true;
        });
    });
});

您可以在这里找到更多信息

演示: jsfiddle

设置提交按钮的ID:

  <input type="submit" name="share" value="Share" id="btn_submit" />

那么您可以像这样在jquery中控制它:

$("#btn_submit").onclick(function(){
    if(condition==true)
    {
        return true;
    }
    else
    {
        return false;
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM