admin管理员组文章数量:1278653
I have a form, and I have some jquery code to override the submit process. But it's not working and there are no errors. I have simplified the sample code, hope I made no typos.
Does anyone know what is wrong with the code? It doesn't e to the point where I get the alert(). The click() event works fine.
I tried setting an id to the form. still nothing.
Using jquery-1.4.2.min.js
<form name="checkout_shipping" action="checkout.php" method="post">
<div class="contentText">
<table border="0" width="100%" cellspacing="0" cellpadding="2">
<tr onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="selectRowEffect(this, 0); document.checkout_shipping.submit();">
<td width="75%" style="padding-left: 15px;">Flat price</td>
<td>$8.00</td>
<td align="right"><input type="radio" name="shipping" value="flat_flat" onclick="this.form.submit();" /></td>
</tr>
<tr onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="selectRowEffect(this, 1); document.checkout_shipping.submit();">
<td>Pick-up</td>
<td>$0.00</td>
<td align="right"><input type="radio" name="shipping" value="pickup_pickup" checked="checked" onclick="this.form.submit();" /></td>
</tr>
</table>
</div>
</form>
<script>
$('form[name=checkout_shipping]').submit(function(e) {
e.preventDefault();
alert('Houston we have contact!');
$.ajax({
url: '/includes/checkout/payment.php',
data: false, // false for testing
type: 'POST',
cache: false,
async: false,
dataType: 'html',
error: function(jqXHR, textStatus, errorThrown) {
// ...
},
success: function(data) {
// ...
}
});
});
</script>
I have a form, and I have some jquery code to override the submit process. But it's not working and there are no errors. I have simplified the sample code, hope I made no typos.
Does anyone know what is wrong with the code? It doesn't e to the point where I get the alert(). The click() event works fine.
I tried setting an id to the form. still nothing.
Using jquery-1.4.2.min.js
<form name="checkout_shipping" action="checkout.php" method="post">
<div class="contentText">
<table border="0" width="100%" cellspacing="0" cellpadding="2">
<tr onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="selectRowEffect(this, 0); document.checkout_shipping.submit();">
<td width="75%" style="padding-left: 15px;">Flat price</td>
<td>$8.00</td>
<td align="right"><input type="radio" name="shipping" value="flat_flat" onclick="this.form.submit();" /></td>
</tr>
<tr onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="selectRowEffect(this, 1); document.checkout_shipping.submit();">
<td>Pick-up</td>
<td>$0.00</td>
<td align="right"><input type="radio" name="shipping" value="pickup_pickup" checked="checked" onclick="this.form.submit();" /></td>
</tr>
</table>
</div>
</form>
<script>
$('form[name=checkout_shipping]').submit(function(e) {
e.preventDefault();
alert('Houston we have contact!');
$.ajax({
url: '/includes/checkout/payment.php',
data: false, // false for testing
type: 'POST',
cache: false,
async: false,
dataType: 'html',
error: function(jqXHR, textStatus, errorThrown) {
// ...
},
success: function(data) {
// ...
}
});
});
</script>
Share
Improve this question
edited Feb 3, 2012 at 0:28
tim
asked Feb 3, 2012 at 0:23
timtim
2,7323 gold badges32 silver badges48 bronze badges
2 Answers
Reset to default 5The way you're submitting the form bypasses the event handelers.
Instead of:
document.checkout_shipping.submit();
Use
$(this).closest('form').submit();
See http://jsfiddle/6uZuS/
Full code:
<form name="checkout_shipping" action="checkout.php" method="post">
<div class="contentText">
<table border="0" cellspacing="0" cellpadding="2">
<tr onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="selectRowEffect(this, 0); $(this).closest('form').submit();">
<td width="75%" style="padding-left: 15px;">Flat price</td>
<td>$8.00</td>
<td align="right"><input type="radio" name="shipping" value="flat_flat" onclick="$(this).closest('form').submit();" /></td>
</tr>
<tr onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="selectRowEffect(this, 1); $(this).closest('form').submit();">
<td>Pick-up</td>
<td>$0.00</td>
<td align="right"><input type="radio" name="shipping" value="pickup_pickup" checked="checked" onclick="$(this).closest('form').submit();" /></td>
</tr>
</table>
</div>
</form>
<script type="text/javascript">
$(document).ready(function(){
$('form[name=checkout_shipping]').submit(function(e) {
e.preventDefault();
alert('Got you!');
});
});
</script>
You can check here or
<form name="checkout_shipping" id="checkout_shipping" action="checkout.php" method="post">
<div class="contentText">
<table border="0" width="100%" cellspacing="0" cellpadding="2">
<tr onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="selectRowEffect(this, 0);">
<td width="75%" style="padding-left: 15px;">Flat price</td>
<td>$8.00</td>
<td align="right"><input type="radio" name="shipping" value="flat_flat" onclick="$(this).parents('form').submit();" /></td>
</tr>
<tr onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="selectRowEffect(this, 1);">
<td>Pick-up</td>
<td>$0.00</td>
<td align="right"><input type="radio" name="shipping" value="pickup_pickup" checked="checked" onclick="$(this).parents('form').submit();" /></td>
</tr>
</table>
</div>
</form>
<script>
$(document).ready(function(){
$('#checkout_shipping').submit(function(e) {
e.preventDefault();
alert('Houston we have contact!');
});
})
</script>
本文标签: javascriptjQuery submit()event to override form actionStack Overflow
版权声明:本文标题:javascript - jQuery submit()-event to override form action - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741278483a2369868.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论