我正在尝试获取名字和姓氏连接的返回值。我基本上需要连接表单中的输入字段以在为空时返回消息,或者如果用户将此信息提供到字段中则返回名字和姓氏。
#message
是我在 HTML 中的 div 标签。
它返回值,但不返回消息“请输入您的名字和姓氏”。如果输入框为空。
这是我的 JavaScript 代码:
var getName = function() {
var firstName = document.getElementById("firstName"),
lastName = document.getElementById("lastName"),
fullName;
if (firstName.value == "" && lastName.value == "" ) {
fullName= $("#message").empty().append("Please enter your first and last name.");
} else {
fullName = firstName.value + " " + lastName.value;
}
return $("#message").empty().append(fullName);
}
$(document).ready(function(){
$("#message").empty().append("Hello world");
});
Here is the HTML for the form
<form name="contactform" method="post" action="">
<input type="text" name="firstName" id="firstName" placeholder= "First Name">
<br>
<input type="text" name="lastName" id="lastName" placeholder= "Last Name">
<br>
<input type="text" name="email" placeholder= "Email Address">
<p>
<h4> What is your sex? </h4>
<input type="radio" name="sex" value="male">
Male
<input type="radio" name="sex" value="female">
Female
<p>
<h4> What is your favorite type of music </h4>
<input type="checkbox" name="vehicle" value="rock">
I like Rock and Roll<br/>
<input type="checkbox" name="vehicle" value="funk">
I like Funk
<p>
<textarea name="comments" placeholder= "Additional Comments"></textarea>
<input type="button" name="submit" id="submit" value="Get Name" onclick="getName();" />
</form>