简体   繁体   English

在JavaScript函数中使用html

[英]Use html in JavaScript function

I face a little problem in below code. 我在下面的代码中遇到了一个小问题。 I write below code. 我写下面的代码。 Unable to find out what's wrong in this code : 无法找出这段代码有什么问题:

HTML: HTML:

<div class="all">
    <div class="info">
        <label>Degree <input type="text" class="row[0][degree]"></label>
        <label>School <input type="text" class="row[0][school]"></label>
        <label>Grade <input type="text" class="row[0][grade]"></label>
    </div>
</div>
<button class="add_button" onclick="add_new()">Add Another</button>

JavaScipt: JavaScipt:

var counter = 1;

function add_new() {

    $('.all').append('
                <div class="info">/
                        <label>Degree <input type="text" class="row[' + counter + '][degree]"></label>/
                        <label>School <input type="text" class="row[' + counter + '][school]"></label>/
                        <label>Grade <input type="text" class="row[' + counter + '][grade]"></label>/
                </div>'
        counter++;
    );
}

Your counter variable doesnt have any relevence, since it is declared locally. 您的counter变量没有任何相关性,因为它是在本地声明的。 If you want to increment the counter each time when calling this function, declare it globally. 如果要在每次调用此函数时递增计数器,请全局声明它。 Also there are some syntax errors in the code, Your code should look like 代码中也有一些语法错误,您的代码应类似于

var counter = 0;
  function add_new() {

    $('.all').append('<div class="info">\
                    <label>Degree <input type="text" class="row[' + counter + '][degree]"></label>\
                    <label>School <input type="text" class="row[' + counter + '][school]"></label>\
                    <label>Grade <input type="text" class="row[' + counter + '][grade]"></label>\
            </div>'

    );

counter++;
}

关闭.append()方法后,尝试增加计数器。

counter has to be global and incremented outside of $.append counter必须是全局的并且在$.append之外增加

var counter = 0;
function add_new(){
    $('.all').append('<div class="info"><label>Degree <input type="text" class="row['+counter+'][degree]"></label><label>School <input type="text" class="row['+counter+'][school]"></label><label>Grade <input type="text" class="row['+counter+'][grade]"></label></div>');                
counter++; 
}

JSFiddle JSFiddle

Instead of using onclick,do it like this with clean way: 与其使用onclick,不如使用干净的方法来做到这一点:

HTML: HTML:

<button class="add_button">Add Another</button>

JQUERY: JQUERY:

$(document).ready(function(){

 var counter = 0;

$(".add_button").click(function(){

            $('.all').append('
            <div class="info">/
                    <label>Degree <input type="text" class="row['+counter+'][degree]"></label>/
                    <label>School <input type="text" class="row['+counter+'][school]"></label>/
                    <label>Grade <input type="text" class="row['+counter+'][grade]"></label>/
            </div>'

            );   
counter++;

   });

})

One way you can append HTML into the DOM without worrying about really long strings is: 将HTML附加到DOM而不用担心字符串很长的一种方法是:

$('.all').append(
    $('<div class="info"/>').html(
        $('<label/>').html( 'Degree ' ).append('<input type="text" class="row[' + counter + '][degree]">')
    )
    .append(
        $('<label/>').html( 'School ' ).append( '<input type="text" class="row[' + counter + '][school]">' )
    )
    .append(
        $('<label/>').html( 'Grade ' ).append( '<input type="text" class="row[' + counter + '][grade]">' )
    )
);

It may be a few more lines, but I find it cleaner, easier to read (when your mind is in JS/jQuery mode) and easier to code .... ... as you can see if there's an object it's quite easy to iterate the object and generate the required HTML 可能还有几行,但是我发现它更简洁,更易于阅读(当您的思维处于JS / jQuery模式时)并且更易于编写代码.....正如您所看到的是否有一个对象,这很容易迭代对象并生成所需的HTML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM