简体   繁体   中英

Getting value from hidden form in jsp to perform jquery post action

Here is my jsp code:

<div align="center">
    <h1>New/Edit Contact</h1>
    <form:form action="saveContact" method="post" modelAttribute="contact">
    <table>
        <form:hidden path="id"/>
        <tr>
            <td>Name:</td>
            <td><form:input path="name" /></td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><form:input path="email" /></td>
        </tr>
        <tr>
            <td>Address:</td>
            <td><form:input path="address" /></td>
        </tr>
        <tr>
            <td>Telephone:</td>
            <td><form:input path="telephone" id="phone" /></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="Save"  onclick = "PostData()"></td>
        </tr>
    </table>
    </form:form>
</div>

On button click i want to get textbox values in some parameter and want to perform Jquery post.

JS:

    function PostData()
    {

        String firstname = request.getParameter("phone");
        alert(firstname);

    }

using jquery ,

function PostData()
{

    var firstname = $('#phone').val();
    alert(firstname);

}

will get the value from the field. where # is the selector to select using id's and the phone is the id of the telephone field .. start with Jquery Tutorial

Its a bit unclear what you are trying to achieve. Do you want to submit all values in one parameter or separately? Do you want to post a form or really make a JS/jQuery POST? However assuming your form will have some ID (myFormID for example)

function PostData() {
    var form = $('#myFormID')[0]; 
    var data = {};
    var dataString = "";

    for(var i=0; i<form.elements.length; i++) {
       var element = form.elements[i];
       data[element.name] = element.value; //assuming the name is set on each element
       dataString = (i>0 ? ";" : "") + element.value;
    }

    $.post("saveContact", data); //OR
    //$.post("saveContact", {mySomeParam:datastring}) // OR
    //alert(data);
    //alert(stringData)
    //form.submit(); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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