我对 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.
我该如何做到这一点?