简体   繁体   中英

Submitting a form using jQuery template

I have this form that loads rows of data in a table format using form. Than user updates some of the amount and submits the form. Form works fine, I don't know how to submit the updated values from html page to jQuery function.

<div id="Sales">
    <script id="salestTemp" type="text/x-jquery-tmpl"> 
        <form id="salesForm" action="">
            <table class="" border="1" cellpadding="0" cellspacing="0">
                <thead>
                    <tr>
                        <th class="label"><a href="">Name<span class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></a></th>                
                        <th class="label"><a href="">$ Amount<span class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></a></th>                  
                   </tr>
                </thead>
                <tbody>
                    {{#each items}}
                    <tr class="">
                        <td>{{=name}}</td>
                        <td>$&nbsp;&nbsp;<input type="text" class="{{=id}}" id="Amount" value='{{=Amount}}' /></td>
                    </tr>
                    {{/each}}  
                    <tr>
                        <input type="submit" class="" id="submit" name="" value="Save" />
                    </tr>
                </tbody>           
            </table>                 
        </form>
    </script
</div>

I am using following jQuery:

$(document).ready(function () {
    $("#submit").click(function () {
        alert("inside");
        $.ajax {......};
    });
});

When I hit submit it doesn't even hit .click function. Anyone knows how to fix this or where to look? Thanks

It has hit the click function check the error console in the browser.. and script tag is not ended properly ""..

And y have you put the form content inside script tag???

Your example has a broken closing tag:

</script
</div>

Consider binding to the submit event:

$("#salesForm").on('submit', function (e) {
    e.preventDefault();
    alert("inside");
});

e.preventDefault(); will prevent the form from submitting, you can then use ajax to submit the form.

Here is an example based on your code: http://jsfiddle.net/n1ck/Jbbhe/2/

Just bind your form to submit

    $(document).ready(function(){
    $('#salesForm_form').bind('submit',function(){
        ...
    });
});

Or

$('#salesForm_form').submit(function(ev) {
    ev.preventDefault(); // to stop the form from submitting
    /* Validations go here */
    this.submit(); // If all the validations succeeded
});

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