admin管理员组文章数量:1022670
I'm using jQuery in conjunction with the form plugin and I'd like to intercept the form data before submission and make changes.
The form plugin has a property called beforeSubmit that should do this, but I seem to be having trouble getting the function I specify to run.
Here's the markup for the form (some style details omitted):
<form id="form1">
<fieldset id="login">
<legend>Please Log In</legend>
<label for="txtLogin">Login</label>
<input id="txtLogin" type="text" />
<label for="txtPassword">Password</label>
<input id="txtPassword" type="password" />
<button type="submit" id="btnLogin">Log In</button>
</fieldset>
</form>
And here's the javascript that I have so far:
$(document).ready(function() {
var options = {
method: 'post',
url: 'Login.aspx',
beforeSubmit: function(formData, form, options) {
$.each(formData, function() { log.info(this.value); });
return true;
}
};
$('form#form1').ajaxForm(options);
});
(log.info() is from the Blackbird debugger library I'm using)
When I click the submit button, rather than the POST verb I specified it uses a GET instead, and nothing is logged from my beforeSubmit function. It seems that the ajaxForm plugin is not being applied to the form at all, but I don't see why. Can anybody help with this?
I'm using jQuery in conjunction with the form plugin and I'd like to intercept the form data before submission and make changes.
The form plugin has a property called beforeSubmit that should do this, but I seem to be having trouble getting the function I specify to run.
Here's the markup for the form (some style details omitted):
<form id="form1">
<fieldset id="login">
<legend>Please Log In</legend>
<label for="txtLogin">Login</label>
<input id="txtLogin" type="text" />
<label for="txtPassword">Password</label>
<input id="txtPassword" type="password" />
<button type="submit" id="btnLogin">Log In</button>
</fieldset>
</form>
And here's the javascript that I have so far:
$(document).ready(function() {
var options = {
method: 'post',
url: 'Login.aspx',
beforeSubmit: function(formData, form, options) {
$.each(formData, function() { log.info(this.value); });
return true;
}
};
$('form#form1').ajaxForm(options);
});
(log.info() is from the Blackbird debugger library I'm using)
When I click the submit button, rather than the POST verb I specified it uses a GET instead, and nothing is logged from my beforeSubmit function. It seems that the ajaxForm plugin is not being applied to the form at all, but I don't see why. Can anybody help with this?
Share Improve this question asked Dec 5, 2008 at 16:44 Adam LassekAdam Lassek 35.5k14 gold badges93 silver badges109 bronze badges2 Answers
Reset to default 3I ran the following code through firebug and it appears to work as advertised, but the formData variable in the beforeSubmit callback is empty because you didn't set the name attribute on the text boxes.
<script type="text/javascript">
$(document).ready(function() {
var options = {
beforeSubmit: showData
};
$('form#form1').ajaxForm(options);
});
function showData(formData, form, options) {
//var formData = [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ];
$.each(formData, function(i, obj) { log.info(obj.name + " | " + obj.value); });
return true;
}
</script>
<form id="form1" action="Login.aspx" method="post">
<fieldset id="login">
<legend>Please Log In</legend>
<label for="txtLogin">Login</label>
<input id="txtLogin" type="text" name="User" />
<label for="txtPassword">Password</label>
<input id="txtPassword" type="password" name="Pass" />
<button type="submit" id="btnLogin">Log In</button>
</fieldset>
</form>
For starters, according to this API, your options object should use type
and not method
, or just specify the method attribute on the form in the HTML.
(now I'll throw in a couple minor stylistic notes; you can stop reading here if you want):
- You can replace
$(document).ready(function...
with just$(function...
. $.each(formData, function...
looks more natural as$(formData).each(function...
I'm using jQuery in conjunction with the form plugin and I'd like to intercept the form data before submission and make changes.
The form plugin has a property called beforeSubmit that should do this, but I seem to be having trouble getting the function I specify to run.
Here's the markup for the form (some style details omitted):
<form id="form1">
<fieldset id="login">
<legend>Please Log In</legend>
<label for="txtLogin">Login</label>
<input id="txtLogin" type="text" />
<label for="txtPassword">Password</label>
<input id="txtPassword" type="password" />
<button type="submit" id="btnLogin">Log In</button>
</fieldset>
</form>
And here's the javascript that I have so far:
$(document).ready(function() {
var options = {
method: 'post',
url: 'Login.aspx',
beforeSubmit: function(formData, form, options) {
$.each(formData, function() { log.info(this.value); });
return true;
}
};
$('form#form1').ajaxForm(options);
});
(log.info() is from the Blackbird debugger library I'm using)
When I click the submit button, rather than the POST verb I specified it uses a GET instead, and nothing is logged from my beforeSubmit function. It seems that the ajaxForm plugin is not being applied to the form at all, but I don't see why. Can anybody help with this?
I'm using jQuery in conjunction with the form plugin and I'd like to intercept the form data before submission and make changes.
The form plugin has a property called beforeSubmit that should do this, but I seem to be having trouble getting the function I specify to run.
Here's the markup for the form (some style details omitted):
<form id="form1">
<fieldset id="login">
<legend>Please Log In</legend>
<label for="txtLogin">Login</label>
<input id="txtLogin" type="text" />
<label for="txtPassword">Password</label>
<input id="txtPassword" type="password" />
<button type="submit" id="btnLogin">Log In</button>
</fieldset>
</form>
And here's the javascript that I have so far:
$(document).ready(function() {
var options = {
method: 'post',
url: 'Login.aspx',
beforeSubmit: function(formData, form, options) {
$.each(formData, function() { log.info(this.value); });
return true;
}
};
$('form#form1').ajaxForm(options);
});
(log.info() is from the Blackbird debugger library I'm using)
When I click the submit button, rather than the POST verb I specified it uses a GET instead, and nothing is logged from my beforeSubmit function. It seems that the ajaxForm plugin is not being applied to the form at all, but I don't see why. Can anybody help with this?
Share Improve this question asked Dec 5, 2008 at 16:44 Adam LassekAdam Lassek 35.5k14 gold badges93 silver badges109 bronze badges2 Answers
Reset to default 3I ran the following code through firebug and it appears to work as advertised, but the formData variable in the beforeSubmit callback is empty because you didn't set the name attribute on the text boxes.
<script type="text/javascript">
$(document).ready(function() {
var options = {
beforeSubmit: showData
};
$('form#form1').ajaxForm(options);
});
function showData(formData, form, options) {
//var formData = [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ];
$.each(formData, function(i, obj) { log.info(obj.name + " | " + obj.value); });
return true;
}
</script>
<form id="form1" action="Login.aspx" method="post">
<fieldset id="login">
<legend>Please Log In</legend>
<label for="txtLogin">Login</label>
<input id="txtLogin" type="text" name="User" />
<label for="txtPassword">Password</label>
<input id="txtPassword" type="password" name="Pass" />
<button type="submit" id="btnLogin">Log In</button>
</fieldset>
</form>
For starters, according to this API, your options object should use type
and not method
, or just specify the method attribute on the form in the HTML.
(now I'll throw in a couple minor stylistic notes; you can stop reading here if you want):
- You can replace
$(document).ready(function...
with just$(function...
. $.each(formData, function...
looks more natural as$(formData).each(function...
本文标签: javascriptModifying form data before submissionStack Overflow
版权声明:本文标题:javascript - Modifying form data before submission - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745528076a2154621.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论