jquery,运行形式

时间:2010-08-17 06:09:56

标签: jquery

如何激活按钮。如果条件为真,我想通过方法(POST)发送数据。

<script>
$(document).ready(function(){                   
    var count = 0;
    if(count == 2)
    {
        //run form
    }
});
</script>
</head>                                                                 
<body>
<form action = "" method = "post">
<input type = "text" name = "name" />
<input type = "submit" id = "ok"/>
</form>

2 个答案:

答案 0 :(得分:3)

$(document).ready(function(){                   
    var count = 0;
    if(count == 2)
    {
        $("myform").submit(function(e){
             //This function is called before form is posted
             if(mycondition == true)
                return true;   //that will post the form
             else
                 return false; // that will stop posting form 

               //Here you can also change values of input fields and also do  
               // validation on them.
               // instead of return false you can also use e.preventDefault() 
               //  that will stop this event to do what it usually does that
                // is submit the form.
           }).trigger("submit"); //if you want it to submit immediatly
        }
    });

              

或者,如果您不想使用表单ID,那么

 $(document).ready(function(){                   
        var count = 0;
        if(count == 2)
        {
            $("body form").find(":input[type=submit][id=ok]").trigger("click");
        }
    });

    <form action = ""  method = "post">
    <input type = "text" name = "name" />
    <input type = "submit" id = "ok"/>
    </form>

答案 1 :(得分:1)

尝试

$("#ok").trigger("click");