【发布时间】:2020-01-31 21:13:55
【问题描述】:
我有一个 html/php 代码,如下所示:
想法是单击加号 (+) 按钮并生成一个新行,其中 HTML 存在于 newRow() 函数。 HTML/PHP 代码有 2 个输入框。
html/php 代码:
<?php
$output = array();
$output['en_desc']=$_POST['en_desc'];
$output['code']=$_POST['code'];
$fp = fopen('../feeds/ptp-ess_landing_committees.json', 'w');
fwrite($fp, json_encode($output));
fclose($fp);
if(file_exists('../feeds/ptp-ess_landing_committees.json')){
$data = json_decode(file_get_contents('../feeds/ptp-ess_landing_committees.json'));
}
?>
<?php if($data) { ?>
<form method="post" id="myform" style="text-align: left;">
<!-- Add New Row Button START -->
<div class="plus-minus-button" style="text-align:center;">
<button type="button" onclick="rowAdd()">+</button>
</div>
<!-- Add New Row Button END -->
<div class="house-senate-committee" style="text-align:center; margin-top:15px;"> <!-- Big div START -->
<!-- Code START -->
<?php if (empty($data->code)) { ?>
<input type="text" name="code[]" style="margin-right:10px;" value="">
<?php } else { ?>
<?php foreach ($data->code as $code){ ?> <!-- Line A -->
<input type="text" name="code[]" style="margin-right:10px;" value="<?php if($code) {echo $code;}?>">
<?php }} ?>
<!-- Code END -->
<!-- EN Desc START -->
<?php if (empty($data->en_desc)) { ?>
<input type="text" name="en_desc[]" value="">
<?php } else { ?>
<?php foreach ($data->en_desc as $en_desc){ ?> <!-- Line B -->
<input type="text" name="en_desc[]" value="<?php if($en_desc) {echo $en_desc;}?>">
<?php }} ?>
<!-- EN Desc END -->
</div> <!-- Big div END -->
</form>
<?php } else {
echo 'Cannot read JSON settings file';
}
?>
这是我用来在 JavaScript 中添加 div 的 JavaScript。
<script>
function rowAdd() {
document.querySelector(".house-senate-committee:last-child").insertAdjacentHTML('afterend', newRow());
}
function newRow() {
return `<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
<input type="text" name="code[]" style="margin-right:10px;" value="">
<input type="text" name="en_desc[]" value="">
</div>`;
}
</script>
问题陈述:
在 Line A 和 Line B,我在 div.house 内使用 for 循环 -senate-committee 所以这可能是它没有以我想要的方式显示的原因。我想知道我应该在 Line A/B (for loop) 处进行哪些更改,以便能够以我想要的方式获得 DOM。
上面的 javascript 代码显示了内容,但它没有像这样逐行添加 div 元素:
<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
<input type="text" name="code[]" style="margin-right:10px;" value="ABC">
<input type="text" name="en_desc[]" value="DEF">
</div>
<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
<input type="text" name="code[]" style="margin-right:10px;" value="QWE">
<input type="text" name="en_desc[]" value="RTY">
</div>
它以以下方式添加(这不是我想要的):
<div class="house-senate-committee" style="text-align:center; margin-top:15px;"> <!-- Big div START -->
<input type="text" name="code[]" style="margin-right:10px;" value="ABC">
<input type="text" name="code[]" style="margin-right:10px;" value="QWE">
<input type="text" name="en_desc[]" value="DEF">
<input type="text" name="en_desc[]" value="RTY">
</div> <!-- Big div END -->
【问题讨论】:
-
也许您不应该在
house-senate-committee的最后一个孩子之后添加该行,而是在该类的最后一个div之后添加该行?此外,给定的 JS 代码如何生成包含fr_desc的行,而这在任何其他代码中都不存在? -
@NicoHaase 如果您阅读了我的问题陈述。我认为,它必须与上面代码中 Line A 和 Line B 处的 for 循环有关。
-
@NicoHaase 如果您阅读了我的问题陈述。我认为,它必须与上面代码中 A 行和 B 行的 for 循环有关。 A 行和 B 行有 for 循环,调用所有 input type ="text" 所以这就是为什么我在 div 中获取所有输入类型的原因,这不是我想要的方式。