Html 将表单字段值提交给 Javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18441375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Submit form field values to a Javascript function
提问by Tanuj
I am using Buddy.com's API to build a mobile app with a web Interface for Administrators and User Creation. I am still in the initial stages of this project.
我正在使用 Buddy.com 的 API 来构建一个带有管理员和用户创建 Web 界面的移动应用程序。我仍处于这个项目的初始阶段。
The first page I am trying to build is the User Registration page for the website. This page will have a form asking the users for a username, password, email and some other fields.
我试图建立的第一个页面是网站的用户注册页面。此页面将有一个表单,要求用户输入用户名、密码、电子邮件和其他一些字段。
I am using the javascript form the page http://buddy.com/developers/#UserAccount_Profile_Create
我正在使用页面http://buddy.com/developers/#UserAccount_Profile_Create的 javascript 表单
The function is: function createUserWithName(); And it has some inputs. The function looks like this:
函数是: function createUserWithName(); 它有一些输入。该函数如下所示:
function fixedEncodeURIComponent (str) {
return encodeURIComponent(str).replace(/[!'()*]/g, escape);
}
function createUserWithName(aName, anEmail)
{
var finalURLStr = "https://webservice.buddyplatform.com/Service/v1/BuddyService.ashx";
finalURLStr += "?UserAccount_Profile_Create"; // API Call
finalURLStr += "&BuddyApplicationName=" + fixedEncodeURIComponent("<#Buddy App Name#>");
finalURLStr += "&BuddyApplicationPassword=" + fixedEncodeURIComponent("<#Buddy App Password#>");
finalURLStr += "&NewUserName=" + fixedEncodeURIComponent(aName); // The user name
finalURLStr += "&UserSuppliedPassword=" + fixedEncodeURIComponent("<#password#>"); // User password
finalURLStr += "&NewUserGender=" + "male"; // "male" or "female"
finalURLStr += "&UserAge=" + 29; // user age (int)
finalURLStr += "&NewUserEmail=" + fixedEncodeURIComponent(anEmail); // user email
finalURLStr += "&StatusID=" + -1; // see status table (1-7 or -1)
finalURLStr += "&FuzzLocationEnabled=" + 1; // true / false: location isn't/is reported accurately
finalURLStr += "&CelebModeEnabled=" + 0; // true if user is hidden from non-friends
finalURLStr += "&ApplicationTag="; // app-related info
finalURLStr += "&RESERVED="; // leave empty
$.ajax({ url: finalURLStr })
.done( function(data) {
// Result is a simple string
alert("Result: " + data);
})
.fail(function() {
alert("Error while contacting Buddy!");
});
}
I have created a form which asks for the user to input this information.
我创建了一个表单,要求用户输入此信息。
How can I pass the field data from my form to this function?
如何将表单中的字段数据传递给此函数?
This is the code for my form:
这是我的表单的代码:
<form id="register">
<table width="200" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><label for="username">Username*:</label>
<input type="text" id="username" name="username" />
</td>
</tr>
<tr>
<td><label for="password">Password*:</label>
<input type="text" id="password" name="password" />
</td>
</tr>
<tr>
<td><label for="email">Email*:</label>
<input type="text" id="email" name="email" />
</td>
</tr>
<tr>
<td><label for="gender">Gender*:</label>
<select id="gender">
<option value="null">Please Choose One...</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
</tr>
</table>
<input class="button" name="submit" type="button" value="submit" onclick="createUserWithName(username.value)" />
</form>
Im sure that once someone shows me how to send the username and email to the javascript function I will be able to figure out the rest.
我敢肯定,一旦有人向我展示了如何将用户名和电子邮件发送到 javascript 函数,我将能够弄清楚其余部分。
Thank you
谢谢
回答by Aashray
You can just use:
你可以只使用:
document.getElementById('username').value
document.getElementById('username').value
in your javascript function. Alert the value to check if you are getting it.
在您的 javascript 函数中。提醒值以检查您是否收到它。
回答by Girish Thimmegowda
first create a function in which you collect all the form data
首先创建一个函数,您可以在其中收集所有表单数据
function getFormData(){
var name=document.getElementById('username').value;
var email=document.getElementById('email').value;
/* some other fields */
/* now call ur function by passing the above values */
createUserWithName(name, email);
}
In your form call getFormData()
method
在您的表单调用getFormData()
方法中
<input class="button" name="submit" type="submit" value="submit" onclick="getFormData()" />
回答by Anup Singh
var username = $("#username").val();
var email= $("#email").val();
createUserWithName(username , email);
回答by kangoroo
there are many ways, but you can use jquery stuff
有很多方法,但你可以使用 jquery 的东西
username = $("username").val();