0

好的,所以当用户在字段中输入他/她的全名时,我想打招呼说,“很高兴认识你,(拆分名称)。我想取输入的值,取 innerHTML,拆分它,然后取第一部分([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;  

  }
4

1 回答 1

3

input元素没有.innerHTML。改用.value

var name = document.getElementById("name"),
    full_name = name.value,
    full_name_split = full_name.split(" ")[0];
于 2013-07-10T06:48:29.957 回答