简体   繁体   English

jQuery动态表输入

[英]jQuery dynamic table inputs

I'm working on a school project with JS, and I've created a form where I enter the amount of forces in a problem and (through jquery) the HTML is edited so that a number of new input rows are added after the row where I entered my number of forces. 我正在用JS进行学校项目,我创建了一个表格,可以在其中输入问题中的作用力,并(通过jquery)编辑HTML,以便在该行之后添加许多新的输入行我输入部队人数的地方

My issue: now that these new inputs are available, I am trying to set them equal to an array so I can use those values later. 我的问题:既然这些新输入可用,我正在尝试将它们设置为等于数组,以便以后可以使用这些值。 I've set up a for loop that works the same as my loop for my number of forces, and I am trying to get it to record each force through clicking the buttons in the new rows. 我已经建立了一个for循环,其作用力与我的力循环相同,并且我试图通过单击新行中的按钮来获取它来记录每个力。 The issue is that it is not recording. 问题是它没有录制。

Anyone have any ideas on how I can get these values in an array? 任何人都对如何获取数组中的这些值有任何想法? It would be very much appreciated. 将不胜感激。

Also, if any of you know of a framework for a dynamic form or table which would make this easier. 另外,如果您知道动态表格或表格的框架,这将使此操作变得容易。

function forceRecording(numofforces) {
    for(var i =0; i<numofforces; i++) {
        $('#button1').parent().parent().after("<tr><td>Force DFO " + i +":</td><td><form><input type='text' name='lengthMeasure'/></form></td><td><div class='button' id='lengthButton"+i+"'>Add!</div></td></tr>")
        $('#button2').parent().parent().after("<tr><td>Force " + i +":</td><td><form><input type='text' name='forceMeasure'/></form></td><td><div class='button' id='forceButton"+i+"'>Add!</div></td></tr>")
    }

    for(var i = 0; i < numofforces;i++) {
        $("#forceButton"+i).click(function(){
            force[i]= $('select[name=forceMeasure]').val();
        });
        $('#forceButton'+i).after("<p>"+force[i]+"</p>");  //only temporary to check if the force is actually in the variable
    }
};

You can use more general selectors to select all of the inputs of that you need in one event. 您可以使用更多常规选择器来选择一个事件中所需的所有输入。

function forceRecording(numofforces){
for(var i =0; i<numofforces; i++){
    $('#somediv').after("<tr><td>Force DFO " + i +":</td><td><input type='text' id='lengthMeasure"+i+"'/></td><td><div class='button' id='lengthButton_"+i+"'>Add!</div></td></tr>")
    $('#someotherdiv').after("<tr><td>Force " + i +":</td><td><input type='text' id='forceMeasure_"+i+"'/></td><td><div class='button' id='forceButton_"+i+"'>Add!</div></td></tr>")
}
};   
$("div.button[id^='forceButton']").live("click",function(){
    var num = parseInt($(this).attr("name").split("_")[1]);
    force[num]=$('text[id=forceMeasure_'+num+']').val();
    console.log(force);
});

Live is a better function to use if you're generating new elements to attach events to. 如果您要生成附加事件的新元素,则使用Live是更好的功能。 You only need to bind events once using live rather than every time an element is created. 您只需要使用live绑定一次事件,而不是每次创建元素时都绑定事件。 You also only need one form element per page. 每页只需要一个表单元素。 Name and ids should ideally always be unique. 理想情况下,名称和ID应该始终是唯一的。

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

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