-1

使用 jQuery,我找到了这样的解决方案:

$("#submit").click(function () {
    alert($('form1').serialize());
    $.post("submitForm", $("form1").serialize(), function (result) {
        if (result == "success") {
            alert("Record added!");
        } else {
            alert("Could not add Record!");
        }
    });
});

这是我上面描述的方法的完整 HTML,我试图找到从 HTML 表单获取数据并将其发送到 servlet“submitForm”的最佳方法。

<html>
 <head>
<title>myForm</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">

    $("#submit").click(function () {
    alert($('form1').serialize());
    $.post("submitForm", $("wmform_174835").serialize(), function (result) {
        if (result == "success") {
            alert("Record added!");
        } else {
            alert("Could not add Record!");
        }
    });
});

</script>
</head>
<body>
<a href="submitForm">MyForm</a>
<form action="submitForm" method="POST" id="form1">
    <input type="text" value="12" name="ID"/>
    <input type="text" value="NameThatComesFirst" name="FirstName"/>
    <input type="text" value="NameThatComesLast" name="LastName"/>
    <input type=submit id="submit" value="submit"/>
</form>
</body>
</html> 
4

1 回答 1

1

尝试这个 -

   $(document).ready(function(){
     $("#submit").click(function () 
       var data = $('#form1').serialize();
       alert(data);
       $.post("submitForm", data, function (result) {
            if (result == "success") {
              alert("Record added!");
            } else {
                alert("Could not add Record!");
            }
       });
    });
   });
于 2013-05-08T16:03:19.200 回答