开发者

why are names not being added to the list?

I found this fiddle and I am trying to get it to work...I can not figure out why the names are not being added to the list, for some reason Add button is acting like a submit button and I can not tell why...It should add all the numbers to a list so when I click submit, then it should send the numbers in an array..

JavaScript:

function bindName() {
    var inputNames = document.getElementById("names").getElementsByTagName("inputNames");
    for (i = 0; i < inputNames.length; i++) {
        inputNames[i].onkeydown = function() {
            if (this.value == "") {
                setTimeout(deletename(this), 1000);
            }
        }
    }
}
document.getElementById("addName").onclick = function() {

    var num1 = document.getElementById("name");

    var myRegEx = /^[0-9]{10}$/;


    var myRegEx = /^[0-9]{10}$/;
    var itemsToTest = num1.value;


    if (myRegEx.test(itemsToTest)) {


        var form1 = document.getElementById("names");

        var nameOfnames = form1.getElementsByTagName("inputNames").length;

        var newGuy1 = document.createElement("inputNames");

        newGuy1.setAttribute("id", nameOfnames);
        newGuy1.setAttribute("type", "text");
        newGuy1.setAttribute("value", num1.value);

        form1.appendChild(newGuy1);
        num1.value = "";
        bindName();

    }
    else {
        alert('error');
    }


};

HTML:

<h1>Enter Name</h1>
<div id="mainName">
    <h2>name</h2>
    <label for="name">Add Names: </label>
    <input id="name" type="text开发者_运维技巧">
    <button id="addName">Add</button>
    <form>
        <div id="names">

        </div>
        <input METHOD="POST" action="text.php" type="submit" value="Submit">
    </form>
</div>


I've seen

document.createElement("inputNames");

Shouldn't be

document.createElement("input");

?


Because this /^[0-9]{10}$/; will accept only 10 numbers and only that, try entering 1234567890 and you will see no error.


I'm not sure why your "name" field is restricted to 10 digit numbers, but I've got the thing to work. http://jsfiddle.net/y8Uju/4/

I think the problem was that you were trying to create an element with the tag name inputNames, but that's not a valid tag. Instead I changed it to create inputs, and set the class to inputNames.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜