从HTML字段中提取值,然后使用get.element

时间:2018-10-20 07:36:39

标签: javascript html variables field

我将如何从字段中提取用户字段信息并将其存储在变量中? 我需要它然后将该信息输出到div中,以供员工记录数据库之类的东西。

    <!DOCTYPE html>
<html>
<head>
    <title>52DA session 5</title>
    <meta charset="utf-8" />
    <link href="https://fonts.googleapis.com/css?family=Quicksand" rel="stylesheet" />
    <link rel="stylesheet" type="text/css" href="../css/employee_styles.css"/>
</head>
<body>
    <div id="container">
        <header>
            <h1 id="heading" class="blueTxt">Employee Records</h1>
        </header>
        <div class="left">
        <form>
            <fieldset>
                <legend><h2>Employee Details Entry</h2></legend>            
                <p class=><label>Name: <input class="text" type="text" id="name" /></label></p>
                <p><label>Age: <input class="text" type="text" id="age" /></label></p>
                <p><label>Position: <input class="text" type="text" id="position" /></label></p>
                <p><label>Details: <textarea type="text" id="details" maxlength="114" ></textarea></label></p>              
                <input class="button" type="button" id="addRecord" onclick="" value="Add Record"/>
            </fieldset>
        </form>             

            <div class="sort">
                <h3>Sort</h3>
                <button class="button" id="sortByName">By Name</button>
                <button class="button" id="sortByAge">By Age</button>
                <br/><button class="button" id="reset">Reset Details</button>
                <br /><br />
            </div>
        </div>
        <section>
            <div id="employeeRecords">

            </div>
        </section>

    </div>  

    <script src="../js/employee_script.js"></script>
</body>
</html>

这是我的空js

var employees = [];



//--------------------------------------------------------------------------------------------------------------

function Person(name, age, pos, dtls, img){

    this.fullName = name;
    this.employeeAge = age;
    this.position = pos;
    this.details = dtls;

    this.avatarSrc = img;

        this.createProfile = function(){
        var profile = document.createElement("div");
        profile.className = "profileStyle";
        var avatar = document.createElement("img");
        avatar.src = this.avatarSrc;
        avatar.alt = this.fullName();
        profile.appendChild(avatar);
        var profileTxt = document.createElement("p");
        profileTxt.innerHTML = "<b>" + this.fullName() +
        "</b><br />" + this.age + 
        "</b><br />" + this.pos + 
        "</b><br />" + this.dtls;
        profile.appendChild(profileTxt);
        return profile;
        }

    employees.push(this);

请使其尽可能简单

欢呼!任何帮助将不胜感激! 我对js和编码一般还很陌生。

2 个答案:

答案 0 :(得分:0)

用于从要获取价值的HTML表单字段获取价值

var x = document.getElementById("myText").value;

,您可以将其设置为要设置的位置

document.getElementById("result").value = x;

答案 1 :(得分:0)

我同意@Rao,您可以阅读这篇文章,讨论如何获取输入字段...

How do I get the value of text input field using JavaScript?

您可以从mozilla网站阅读 https://developer.mozilla.org/it/docs/Web/JavaScript/Guida

还有w3schools ...

https://www.w3schools.com/js/