动态添加文本输入字段

时间:2018-04-29 13:16:31

标签: javascript jquery html

我是Javascript和Jquery的新手,并试图学习如何添加dinamically  我的html中的元素。

我的HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Academic Calculator</title>
    <meta name="Calculator" content="Academic Calculator">
    <link rel="stylesheet" href="Calculate.css">

</head>
<body>
    <script src="CalculatorLogics.js"></script>
    <div>
        <form>
                First Name:<br>
                <input name="first-name">
                <br>
                Last Name:<br>
                <input name="last-name">
                <br>
            <hr>
        </form>
    </div>
    <div class="courses">
        <button type="button" onclick="createNewCourses()">Add Course</button>
    </div>
</body>
</html>

和我的Javascript:

function createNewCourses() {
    $(".courses").append("<input type='text>");
}

1 个答案:

答案 0 :(得分:1)

您错误'text替换为"text"

function createNewCourses() {
    $('.courses').append('<input type="text">');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


   <div>
        <form>
                First Name:<br>
                <input name="first-name">
                <br>
                Last Name:<br>
                <input name="last-name">
                <br>
            <hr>
        </form>
    </div>

    <div class="courses">
        <button type="button" onclick="createNewCourses()">Add Course</button>
    </div>