replace action attribute value in form
I am trying to change开发者_运维技巧/replaced the action attribute in a form but am having a problem with the selector. I want to change the action to ShoppingCart.asp?recal=Y
Here is the markup for the form.
<form onsubmit="if (typeof(Update_Hidden_State_Fields)=='function') Update_Hidden_State_Fields(); if (this.submitted) return false; else {this.submitted=true; return true;}" action="ShoppingCart.asp" name="form" method="POST">
Here is the Jquery I have so far.
$("[name='form']")
.attr('action', function(jj,ww){
return ww.replace('ShoppingCart.asp','ShoppingCart.asp?recal=Y');})
$("[name=form]").attr("action", "ShoppingCart.asp?recal=Y");
Why not the easy way:
$("[name=form]").attr('action','ShoppingCart.asp?recal=Y');
?
What you have works, give it a try here: http://jsfiddle.net/nick_craver/Pgt5D/
I think you're missing is the document.ready
wrapper, like this:
$(function() {
$("[name='form']").attr('action', function(jj,ww){
return ww.replace('ShoppingCart.asp','ShoppingCart.asp?recal=Y');
});
});
Without running it on document.ready
, unless your script is running after that form element in the page, it just won't find any elements to run .attr()
on. By wrapping it in $(function() { });
or $(document).ready(function() { });
you're telling it to wait until the DOM is ready, and the element will be there. Also I suggest changing your selector to $("form[name='form']")
(if there are other forms with names) to make it much more efficient.
精彩评论