I am trying to solve an issue I have where I need to grab the action="/uri/" from the parent form when a user clicks the submit button. If I use $(form).live('submit') it only works half the time. So I am using $('input[type=""]').live('click') instead. However as you can see in the code below, I am trying to grab the action attribute and it does not recognize it, or rather I can’t seem to grab the .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 that I am trying to use looks like this (removed PHP for 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 the HTML isn’t the best and I will need to go back and revise it a bit.
Your HTML is invalid.
<tr></tr>elements can only contain<td></td>and<th></th>elements. Invalid html will have inconsistent results across browsers or possibly not work at all in any browser.https://developer.mozilla.org/en/HTML/Element/tr
Wrap the form around the entire table instead of each tr, and give each tr a data-key to contain the id of the given tr. With that, you can catch any submit that gets to the form, get the id of the current row by using
$(this).closest('tr').data("key"), then post the values from the inputs within said tr.