使用Javascript发布表单

时间:2013-12-18 22:39:26

标签: javascript jquery forms post action

我有以下表格标签:

<form id="Form1" action="https://www.example.com/" method="post" runat="server">
            <asp:HiddenField ID="p" runat="server" Value="1" />
            <asp:HiddenField ID="msg" runat="server" Value="2"/>
            <asp:HiddenField ID="cha" runat="server" Value="3"/>
            <asp:HiddenField ID="lang" runat="server" Value="4"/>
            <asp:HiddenField ID="number" runat="server" Value="5"/>
            <asp:HiddenField ID="amo" runat="server" Value="6"/>
            <asp:HiddenField ID="cu" runat="server" Value="7"/>
            <asp:HiddenField ID="co" runat="server" Value="8"/>
            <asp:HiddenField ID="cl" runat="server" Value="9"/>
            <asp:HiddenField ID="crl" runat="server" Value="10"/>
            <asp:HiddenField ID="sature" runat="server" Value="11"/>
            <asp:HiddenField ID="mk" runat="server"/>
    <input type="submit" name="submit" value="Go" />
</form>

我希望在页面加载时使用JavaScript触发帖子操作!

我在Head标记中使用了以下Javascript代码来执行此操作,但它无效。

<script type="text/javascript">
    $(document).ready(function () {
        var myForm = document.getElementById('Form1');
        myForm.submit();
    });

</script>

任何建议?????

Edit1:我尝试在this article中提供解决方案,但没有解决我的问题。

Edit2:将以下脚本添加到表单中。 (头标记)。它们足够吗?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js" type="text/javascript"></script>

4 个答案:

答案 0 :(得分:4)

要在页面加载时提交表单,请将脚本标记放在表单后面,不要将其包装在document.ready中(这个答案假设您在页面的头部包含了jquery,因为您有它在你的问题中)

<form id="Form1" action="https://www.example.com/" method="post" runat="server">
        <asp:HiddenField ID="p" runat="server" Value="1" />
        <asp:HiddenField ID="msg" runat="server" Value="2"/>
        <asp:HiddenField ID="cha" runat="server" Value="3"/>
        <asp:HiddenField ID="lang" runat="server" Value="4"/>
        <asp:HiddenField ID="number" runat="server" Value="5"/>
        <asp:HiddenField ID="amo" runat="server" Value="6"/>
        <asp:HiddenField ID="cu" runat="server" Value="7"/>
        <asp:HiddenField ID="co" runat="server" Value="8"/>
        <asp:HiddenField ID="cl" runat="server" Value="9"/>
        <asp:HiddenField ID="crl" runat="server" Value="10"/>
        <asp:HiddenField ID="sature" runat="server" Value="11"/>
        <asp:HiddenField ID="mk" runat="server"/>
        <input type="submit" name="submit" value="Go" />
</form>

<script type="text/javascript">
    $('#Form1').submit();
</script>

答案 1 :(得分:1)

<script type="text/javascript">
    $(document).ready(function () {
        $('#Form1').submit();
    });

</script>

答案 2 :(得分:1)

使用jQuery,在$('#Form1').submit(function(){ \\HERE });

上执行此操作
$.post("https://www.example.com/", $("#Form1").serialize(),  function() {
    alert("success");
}));

所以总的来说:

\\Event that occurs when DOM is ready
$(document).ready(function () {
    \\Event binding that occurs when the element with ID `Form1` is submitted 
    $('#Form1').submit(function(){
        \\Calls the POST function in jQuery with the serialized form data
        $.post("https://www.example.com/", $("#Form1").serialize(),  function() {
            alert("success");
        })); 
    });
});

答案 3 :(得分:1)

如果你使用的是jquery,你可以这样做:

<script type="text/javascript">
    $(document).ready(function () {
        $('#Form1').submit();
    });

</script>