javascript - 计算并自动填充动态元素

标签 javascript jquery html

我正在尝试修复我的时间表单脚本来计算剩余的天数。 目前,它仅计算第一天的进出时间,即持续时间。不确定如何获取第二天以及要计算的数据。

我遇到的另一个问题是无法编辑第一天,因为当我添加新的天数时,我无法再计算持续时间。但是,如果我返回到第一天并删除多余的天数,那么我就可以再次编辑和计算。

附注段就是我所说的天。

		var template;
		var numOfSegments = 1;

		window.addEventListener('load', function() {
			template = document.querySelector("#wrapper").innerHTML;
			document.querySelector("#more_fields").addEventListener("click", function(e) {
				e.preventDefault(); // tell the browser to not send the form
				document.getElementById('wrapper').insertAdjacentHTML('beforeend', template); // add next segment
				numOfSegments = document.querySelectorAll("div.segment").length;
				document.querySelector("div.segment:last-of-type > label").innerHTML = "Day " + (numOfSegments) + ":"; //Updates Segment #
			});
		})


		function deleteMe() {
			if (numOfSegments > 1) {
				var btn = document.querySelector("#wrapper > div.segment:last-of-type");
				btn.remove();
				event.preventDefault();
			}
		}

		function addNumSeg() {
			var elem = document.getElementById("segments_num");
			elem.value = ++numOfSegments;
		}

		function subtractNumSeg() {
			var elem = document.getElementById("segments_num");
			if (numOfSegments > 1) {
				elem.value = --numOfSegments;
			}
		}
	<form id="seg" oninput="z.value=parseInt(segout.value)-parseInt(segin.value)">
		<div id="room_fileds">
			<div class="content" id="wrapper">
				<div class="segment">
					<label id="seg[]" style="margin:0 0 10px 60px;display: inline;">Day 1:</label>
					<div class="form-group" style="display: inline;">
						<label id=seg-in[] style="margin:0 0 10px 35px;display: inline;">IN:</label>
						<input class="form-control seg_in" id="segin" type="text" style="margin:0 0 10px 5px;Width:15%;display: inline;">
					</div>
					<div class="form-group" style="display: inline;">
						<label id=seg-out[] style="margin:0 0 10px 35px;display: inline;">OUT:</label>
						<input class="form-control seg_out" id="segout" type="text" style="margin:0 0 10px 5px;Width:15%;display: inline;">
					</div>

					<div class="form-group" style="display: inline;">
						<label id="seg-dur[]" style="margin:0 0 10px 35px;display: inline;">Duration:</label>
						<output class="form-control seg_out" form="seg" name="z" for="segin segout" style="margin:0 0 10px 5px;Width:15%;display:inline" ; readonly></output>
					</div>
				</div>
			</div>
		</div>

		<div style="text-align:right;">
			<div style="display:inline;text-align: right;">
				<button onclick="deleteMe(); subtractNumSeg();" type="button" style="height: 25px;width:14px;" id="less_fields">-</button>
			</div>

			<div style="display:inline;text-align: right;">
				<button onclick="addNumSeg();" type="button" style="height: 25px;width:14px;" id="more_fields">+</button>
			</div>
		</div>
		<br><br>
		<button type="button" class="btn btn-default" id="formSubmit">Submit</button>

最佳答案

您缺少实际的 segments_num 元素:

  <div id='segments_num' value=1></div>

在 HTML 中修复了这个问题。

我还将 addNumSeg(); 放入事件监听器中以避免异步事件处理问题。

var template;
		var numOfSegments = 1;

		window.addEventListener('load', function() {
			template = document.querySelector("#wrapper").innerHTML;
			document.querySelector("#more_fields").addEventListener("click", function(e) {
				e.preventDefault(); // tell the browser to not send the form
				document.getElementById('wrapper').insertAdjacentHTML('beforeend', template); // add next segment
				numOfSegments = document.querySelectorAll("div.segment").length;
				document.querySelector("div.segment:last-of-type > label").innerHTML = "Day " + (numOfSegments) + ":"; //Updates Segment #
addNumSeg();
			});
		})


		function deleteMe() {
			if (numOfSegments > 1) {
				var btn = document.querySelector("#wrapper > div.segment:last-of-type");
				btn.remove();
				event.preventDefault();
			}
		}

		function addNumSeg() {
			var elem = document.getElementById("segments_num");
			elem.value = ++numOfSegments;
		}

		function subtractNumSeg() {
			var elem = document.getElementById("segments_num");
			if (numOfSegments > 1) {
				elem.value = --numOfSegments;
			}
		}
<div id='segments_num' value=1></div>
	<form id="seg" oninput="z.value=parseInt(segout.value)-parseInt(segin.value)">
		<div id="room_fileds">
			<div class="content" id="wrapper">
				<div class="segment">
					<label id="seg[]" style="margin:0 0 10px 60px;display: inline;">Day 1:</label>
					<div class="form-group" style="display: inline;">
						<label id=seg-in[] style="margin:0 0 10px 35px;display: inline;">IN:</label>
						<input class="form-control seg_in" id="segin" type="text" style="margin:0 0 10px 5px;Width:15%;display: inline;">
					</div>
					<div class="form-group" style="display: inline;">
						<label id=seg-out[] style="margin:0 0 10px 35px;display: inline;">OUT:</label>
						<input class="form-control seg_out" id="segout" type="text" style="margin:0 0 10px 5px;Width:15%;display: inline;">
					</div>

					<div class="form-group" style="display: inline;">
						<label id="seg-dur[]" style="margin:0 0 10px 35px;display: inline;">Duration:</label>
						<output class="form-control seg_out" form="seg" name="z" for="segin segout" style="margin:0 0 10px 5px;Width:15%;display:inline" ; readonly></output>
					</div>
				</div>
			</div>
		</div>

		<div style="text-align:right;">
			<div style="display:inline;text-align: right;">
				<button onclick="deleteMe(); subtractNumSeg();" type="button" style="height: 25px;width:14px;" id="less_fields">-</button>
			</div>

			<div style="display:inline;text-align: right;">
				<button type="button" style="height: 25px;width:14px;" id="more_fields">+</button>
			</div>
		</div>
		<br><br>
		<button type="button" class="btn btn-default" id="formSubmit">Submit</button>

关于javascript - 计算并自动填充动态元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45684228/

相关文章:

php - 从 file_get_contents 更改/删除 html

javascript - 如何在一页中使用具有相同 id 的重复元素的 JavaScript?

javascript - 无法在 Atom 编辑器中使用 eslint

javascript - 用于绘制 MGRS 的叠加层

jquery - 无法专注于标签

javascript - 根据里面的内容改变iframe的高度?

jquery - 固定表格列宽(使用 colgroup)在 IE 和 Safari 中删除列后不起作用

javascript - 如何增加博客片段中的字符长度而不是使用有限的长度 'data:post.snippet' ?

javascript - Array.indexOf 不敏感数据类型

javascript - 处理搜索结果和 Bootstrap 模式