我是 JQuery 新手。我已经弄清楚如何进行 AJAX 调用,以及如何创建成功或失败的回调函数。不过,我在弄清楚如何传递参数时遇到了困难。这是我的 JQUery 代码:
$(document).ready(function(){
$('#submit_new_admin').click(function() {
$.ajax({
type: 'POST',
url: '[mu_url]/process_request.php',
data: { request: 'add_new_admin' },
beforeSend:function(){
$('#ajax-panel').html('<div class="loading"><img src="/images/loading.gif" alt="Loading..." /></div>');
},
success:function(data){
alert('ok')'
},
error:function(){
$('#ajax_response').html('<p class="error"><strong>Oops!</strong> Try that again in a few moments.</p>');
}
});
});
});
效果很好,但我想动态设置参数变量。这是我正在使用的表格:
<form name="" action="" method="post">
<select name="user_level">
<option value="1">Full Admin</option>
<option value="2">Junior Admin</option>
</select><br/><br/>
<label for="firstname">Firstname</label> <input type="text" name="firstname"><br/><br/>
<label for="lastname">Lastname</label> <input type="text" name="lastname"><br/><br/>
<label for="username">Username</label> <input type="text" name="username"><br/><br/>
<label for="fb_id">FaceBook ID</label> <input type="text" name="fb_id"><br/><br/>
<label for="password">Password</label> <input type="password" name="password"><br/><br/>
<label for="password_conf">Enter Password Again</label> <input type="password" name="password_conf"><br/><br/>
<input type="hidden" name="submitted">
<input id="submit_new_admin" type="button" value="Add As Admin">
</form>
我试图用 Jquery 做的是传递文本输入的值。
示例
data: { request: 'add_new_admin', firstname: [firstname value from form], lastname: [lastname value from form], username: [username from form]} and so on.
我该怎么做?
最佳答案
您可以使用 jquery 来填充动态值 -
例如数据: {firstname : $('#firstname').val(), ......}
但是,由于您有一个巨大的表单,您可以使用 jquery 表单序列化 api 通过 ajax 调用将所有表单元素作为 url 或数组传递,而不是填充单个元素。
例如$.post("test.php", $("#testform").serialize());
更多信息请访问serialize & Jquery.post
关于jquery - 将参数传递给 JQuery Ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7456734/