简体   繁体   English

CakePHP-多行表单-使用JS添加输入,但仅第一个输入被提交

[英]CakePHP - multirow form - add inputs with JS, but only the first ones gets submitted

I'm doing a CakePHP site, I have a form there, and the user should be able to add multirow inputs dynamically, with JavaScript. 我正在做一个CakePHP网站,那里有一个表单,用户应该可以使用JavaScript动态添加多行输入。 Let me simplify it: I generate the first input with this code: 让我简化一下:我使用以下代码生成第一个输入:

echo $this->Form->input('Student.0.name');

So, when the user click on add more inputs - then I inject the HTML for this input - which is: 因此,当用户单击添加更多输入时-然后我为此输入注入HTML-即:

<div class="input required">
  <label for="Student1Name">Width</label><input name="data[Student][1][name]" id="Student1Name" type="text">
</div>

The HTML, generated from the CakePHP code is equal to the other one - except the "0" and "1", we have sequent number here. 从CakePHP代码生成的HTML与其他HTML相同-除“ 0”和“ 1”外,我们在此具有后续编号。 But, when I submit - I have only the first input data on the server ... as the other ones have never been submitted. 但是,当我提交时-我的服务器上只有第一个输入数据...因为其他数据从未提交过。

What am I wrong? 我怎么了

// some variables .. 
var currentlyStudentsCount = 1;

var oneStudentHtml = '<div class="input required"><label for="StudentstudentNumberName">Width</label><input name="data[Student][studentNumber][name]" id="StudentstudentNumberCountName" type="text"></div>';

$('#addStudent').on('click', function() {

  // and add the HTML for one more student .. 
  $('#moreStudentsWrapper').append(
    oneStudentHtml.replace(
      /studentNumber/g,
      currentlyStudentsCount
    )
  );

  // increment the current students count .. 
  currentlyStudentsCount++;
});

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

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