好的,所以当用户在字段中输入他/她的全名时,我想要一个问候语,“很高兴见到你,(拆分名称)。我想取输入的值,取内部HTML拆分它,然后取第一部分([0]),并提醒它!
忽略if / else的东西:P
HTML
<form onsubmit="return formValidate(this);">
<label>Full name</label><br>
<input type="text" id="name"/>
</form>
JS
function formValidate(form){
// set initial status
var status = true;
var name = document.getElementById("name"),
full_name = name.innerHTML,
full_name_split = full_name.split(" ")[0];
if(name.value == "") {
document.getElementById("name-alert").innerHTML = alerts[0];
status = false;
} else if(namePatt.test(name.value) == false){
document.getElementById("name-alert").innerHTML = alerts[1];
status = false;
} else if(name.value.indexOf(charSpace) == 0 || name.value.indexOf(charSpace) == -1){
document.getElementById("name-alert").innerHTML = alerts[2];
status = false;
}
alert(full_name_split);
// return the initial status
return status;
}
答案 0 :(得分:3)
input
个元素没有.innerHTML
。请改用.value
:
var name = document.getElementById("name"),
full_name = name.value,
full_name_split = full_name.split(" ")[0];