ajax - How do I get the attribute action from closest form in jQuery? -


i trying solve issue have need grab action="/uri/" parent form when user clicks submit button. if use $(form).live('submit') works half time. using $('input[type=""]').live('click') instead. can see in code below, trying grab action attribute , not recognize it, or rather can't seem grab .attr('action') off of it.

<script> var get_pages_load_options = {      target:        '#content',     beforesubmit:  showrequest,     success:       showresponse }; $('input[type="submit"]').live('click',function(event){     var url = $(this).closest("form").attr('action');     var named = $(this).closest("form").attr('id')     console.debug(url+"/"+named);     $("#"+named).ajaxform(get_pages_load_options);     $.ajax({         type: "post",         url: url,         data: {ajaxtype: "side"},         success: function(data) {             $("#sidebar_menu").html(data);         }     });     event.preventdefault();     return false; }); </script> 

the html trying use looks (removed php readability sake):

<tr>     <td class="bold" colspan="2">         <form id='defcon_form' name='defcon_form' action='/cityhall/' method='post'>             <div class="left">                 realm hoarding                  <select name='defcon_rate'>                     <option value='0'>none</option>                     <option value='10'>low</option>                     <option value='20'>medium</option>                     <option value='30'>high</option>                     <option value='50'>critical</option>                 </select><br />                 <font size='-4'>(stashes gold/turn, decreases defense)</font>             </div><div class="right">                 <input type='submit' name='defcon' value='update hoarding!' />             </div>         </form>     </td> </tr> 

i know html isn't best , need go , revise bit.

your html invalid. <tr></tr> elements can contain <td></td> , <th></th> elements. invalid html have inconsistent results across browsers or possibly not work @ in browser.

https://developer.mozilla.org/en/html/element/tr

wrap form around entire table instead of each tr, , give each tr data-key contain id of given tr. that, can catch submit gets form, id of current row using $(this).closest('tr').data("key"), post values inputs within said tr.


Comments