search

Home  >  Q&A  >  body text

javascript - A question in DEMO

<!DOCTYPE html>
<html>
<head>
    <title>便签</title>
</head>
<body>
    <form>
        <select id="note-level">
            <option value="normal">Normal tip</option>
            <option value="important">Important tip</option>
        </select>
        <input type="text" name="note" placeholder="Input your note here" id="note" required>
        <input type="button" name="submit" id="take-note" value="Take Note">
    </form>
    <ul id="note-list">
    </ul>
    <script type="text/javascript">
        window.onload=init;
        function init(){
            var takeNoteButton=document.getElementById("take-note");
            takeNoteButton.onclick=createNote;
        }
        function createNote(){
            //获取输入的值
            var noteElement=document.getElementById("note");
            var noteValue=noteElement.value;
            noteElement.value="";
            //获取数据存储的层次
            var levelObj=document.getElementById("note-level");
            var index=levelObj.selectedIndex;
            var level=levelObj[index].value;
            //把输入的值和数据存储的层次封装起来
            var noteObj={
                value:noteValue,
                level:level
            }
            var date=new Date();
            var key=date.getTime();
            //保存数据
        }
    </script>
</body>
</html>
各位大神好,请问获取输入值那段javacript中:
var noteElement=document.getElementById("note");
var noteValue=noteElement.value;
noteElement.value="";
在第二行代码中,已经获取了noteValue的值,为啥还要第三行代码: noteElement.value="";还请大神解惑!!!谢谢!!
我想大声告诉你我想大声告诉你2748 days ago462

reply all(1)I'll reply

  • PHPz

    PHPz2017-05-19 10:36:36

    This is to clear the input box after getting the value.
    Why should we clear it?
    Think about it, if you fill in the information and click submit. As a result, the input box has not changed, that is, the information is still there. How does the user know whether the information is uploaded successfully?

    reply
    0
  • Cancelreply