jQuery manipulating forms
For some reason it's not changing the action of the forum. I have some code to change the action of a form when a button is clicked:
function changeForm(event){
alert("Before: "+jQuery("#franchiseform").attr("action"));
jQuery("#franchiseform").attr("action", "franchisepreview.php");
alert("After: "+jQuery("#franchiseform").attr("action"));
jQuery("#franchiseform").submit();
}
The binding:
jQuery("input.preview").bind("click", changeForm);
The form:
<form method="post" enctype="multipart/form-data" action="franchiseinsert.php" class="insert-new" id="franchiseform">
The buttons:
<input type="button" value="Preview" id="preview" name="preview" id="preview" class="preview" /><input type="submit" 开发者_StackOverflowvalue="Insert" />
First off, you need to clean up the
<input type="button" value="Preview" id="preview" name="preview" id="preview" class="preview" />
to:
<input type="button" value="Preview" id="preview" name="preview" />
as an input should not have multiple id attributes and you should not use classes and id's with the same name. This could be one reason why you're having problems. Then I used the following code and the action url was actually changing:
<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#preview").bind("click", changeForm);
function changeForm(event){
alert("Before: "+ $("#franchiseform").attr("action"));
$("#franchiseform").attr("action", "franchisepreview.php");
alert("After: "+ $("#franchiseform").attr("action"));
$("#franchiseform").submit();
}
});
</script>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="franchiseinsert.php" class="insert-new" id="franchiseform">
<input type="button" value="Preview" id="preview" name="preview" />
<input type="submit" value="Insert" />
</form>
</body>
</html>
Unless you stripped out too much, it looks like you need to put your "after" call into a function called "changeForm", which is what you are binding to the click event.
otherwise, keep chaining:
jQuery("input.preview").bind("click", function(){
jQuery("#franchiseform").attr("action", "franchisepreview.php"));
jQuery("#franchiseform").submit();
});
It turns out that this is a Firefox / Mac specific bug. It works on other platforms and browsers, but not this specific computer for some reason. It could be an extension conflict or something else, I'll simply use another browser.
精彩评论