[英]Getting value from input jquery
我試圖從輸入中獲取值並通過腳本傳遞它來注冊用戶,但是沒有運氣來存儲它。 我設置為測試的警報沒有顯示任何值。 這是我必須獲取用戶名的值並將其傳遞到腳本中的內容:
<form id ="form-signin" class="form-signin" action="" method="">
<input type="text" id="username" class="form-control" placeholder="Username" required autofocus>
<input type="email" class="form-control" placeholder="Email" required autofocus>
<input type="password" class="form-control" placeholder="Password" required>
<input type="submit" id="createUser" class="btn btn-lg btn-default btn-block" value="Sign Up" />
</form>
<script type="text/javascript">
Parse.initialize("u3BTp3Efoko8hbNhl5MCeli8Kd2iiEk8mE4vYgn4", "tQEGymAWeB8Tr2LA3YDGoq2Lt2xpGMW9ikeFSTtD");
//get the input data
var username = $('#username').val();
//set the user
var user = new Parse.User();
$( "form" ).submit(function( event ) {
alert( "Error: " + username + " " );
user.set("username", " " + username + " ");
user.set("password", "my pass");
user.set("email", "email@example.com");
// other fields can be set just like with Parse.Object
user.set("phone", "415-392-0202");
user.signUp(null, {
success: function(user) {
// Hooray! Let them use the app now.
},
error: function(user, error) {
// Show the error message somewhere and let the user try again.
alert("Error: " + error.code + " " + error.message);
}
});
});
</script>
您需要在提交處理程序中讀取username的值。
當頁面加載為空白時,您正在讀取#username
字段的值,一旦讀取該值並將其分配給變量,則在更新輸入值時該變量值將不會更新。
//dom ready handler
jQuery(function ($) {
Parse.initialize("u3BTp3Efoko8hbNhl5MCeli8Kd2iiEk8mE4vYgn4", "tQEGymAWeB8Tr2LA3YDGoq2Lt2xpGMW9ikeFSTtD");
//set the user
var user = new Parse.User();
$("form").submit(function (event) {
//get the input data
var username = $('#username').val();//read the user value in s
alert("Error: " + username + " ");
user.set("username", " " + username + " ");
user.set("password", "my pass");
user.set("email", "email@example.com");
// other fields can be set just like with Parse.Object
user.set("phone", "415-392-0202");
user.signUp(null, {
success: function (user) {
// Hooray! Let them use the app now.
},
error: function (user, error) {
// Show the error message somewhere and let the user try again.
alert("Error: " + error.code + " " + error.message);
}
});
});
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.