[英]Add/Delete a row on button-click in Javascript
I have a screenshot as shown below:我有一个截图,如下所示:
The screenshot above belongs to the following code:上面的截图属于以下代码:
<h3 style="text-align:center;margin-top:45px;">Sitting Days</h3>
<div class="sitting-days" style="display:flex; justify-content:center; margin-bottom:20px;">
<!-- Date START -->
<div class="select-date" style="margin-right:30px;">
<h4 style="text-align:center;">Select Date</h4>
<input type="date" id="house-sitting-date" name="house_sitting_date" value="<?php if($data->{" house_sitting_date "}<>''){echo $data->{"house_sitting_date "};}?>">
</div>
<!-- Date END -->
<!-- Yes/No Dropdown START -->
<div class="yes-no">
<h4 style="text-align:center;">Yes/No</h4>
<select name="house_sitting_date_yes_no" id="house-yes-no" style="height:24px;">
<option value="nada" <?php if($data->{"house_sitting_date_yes_no"}=="nada"){echo 'selected';} ?>>Please choose an option</option>
<option value="yes" <?php if($data->{"house_sitting_date_yes_no"}=="yes"){echo 'selected';} ?>>Yes</option>
<option value="no" <?php if($data->{"house_sitting_date_yes_no"}=="no"){echo 'selected';} ?>>No</option>
</select>
</div>
<!-- Yes/No Dropdown END -->
<!-- Add new row button START -->
<div class="add-new-row-button">
<input type="button" id="addRow" value="Add New Row" onclick="rowAdd()"/>
</div>
<!-- Add new row button END -->
</div>
<!-- Javascript Code START -->
<script>
function rowAdd() {
}
</script>
<!-- Javascript Code END -->
On hitting save button, the above form (as shown in the screenshot) gets saved in the following JSON file:点击保存按钮后,上面的表单(如屏幕截图所示)将保存在以下 JSON 文件中:
$output['house_sitting_date']=$_POST['house_sitting_date'];
$output['house_sitting_date_yes_no']=$_POST['house_sitting_date_yes_no'];
if(file_exists('../feeds/ptp-ess_landing_house.json')){
$data = json_decode(file_get_contents('../feeds/ptp-ess_landing_house.json'));
}
Problem Statement:问题陈述:
I am wondering what JavaScript code I need to add so that it add another select of rows .我想知道我需要添加什么JavaScript 代码,以便它添加另一个选择的行。 When I say Select of Rows , I meant to say the following.当我说Select of Rows 时,我的意思是说以下内容。 I added an onclick event on input tag.我在输入标签上添加了一个 onclick 事件。
You'll need to change some minor things, but this is the general idea:您需要更改一些小东西,但这是总体思路:
<h3 style="text-align:center;margin-top:45px;">Sitting Days</h3> <div class="add-new-row-button"> <input type="button" id="addRow" value="Add New Row" onclick="rowAdd()" /> </div> <div id="rows"> <div class="sitting-days" style="display:flex; justify-content:center; margin-bottom:20px;"> <div class="select-date" style="margin-right:30px;"> <h4 style="text-align:center;">Select Date</h4> <input type="date" id="house-sitting-date" name="house_sitting_date" value=""> </div> <div class="yes-no"> <h4 style="text-align:center;">Yes/No</h4> <select name="house_sitting_date_yes_no" id="house-yes-no" style="height:24px;"> <option value="nada" selected>Please choose an option</option> <option value="yes">Yes</option> <option value="no">No</option> </select> </div> </div> </div> <script> function rowAdd(event) { document.getElementById("rows") .insertAdjacentHTML('beforeend', newRow()); } function newRow() { return ` <div id="default-node" class="sitting-days" style="display:flex; justify-content:center; margin-bottom:20px;"> <div class="select-date" style="margin-right:30px;"> <input type="date" id="house-sitting-date" name="house_sitting_date" value=""> </div> <div class="yes-no"> <select name="house_sitting_date_yes_no" id="house-yes-no" style="height:24px;"> <option value="nada" selected>Please choose an option</option> <option value="yes">Yes</option> <option value="no">No</option> </select> </div> </div> ` } </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.