javascript - 添加时重复输入

标签 javascript html css

该脚本的想法是向其中添加部分和文本框。如果我添加一个部分并向其中添加输入,则所有内容都会播放。然而,当我添加另一个时 - 其中一个提示是重复的,我不明白为什么,我输入代码来检查我的意思。

$("#add_section").on("click", function() {
			
			var sectionid = $(".sekcja").length;
			$("#sectionid").val(sectionid);
			var sectionwidth = prompt("Section width");
			$("#sectionwidth").val(sectionwidth);
			var sectionheight = prompt("Section height");
			$("#sectionheight").val(sectionheight);
			var bg = prompt("Section background color");
			$("#bg").val(bg);
			var sectioncolor = prompt("Section font color");
			$("#sectioncolor").val(sectioncolor);
			$("#new_section").append('<div class="section" style="width: '+ sectionwidth +'px; min-height: '+ sectionheight +'px; background: #'+ bg +'; color: #'+ sectioncolor +';"><button type="button" class="add_text">Add text</button></div>');
			$(".add_text").on("click", function() {
				
				var inputid = $('.sample').length;
				$("#inputid").val(inputid);
				var inputwidth = prompt("Width text area");
				$("#inputwidth").val(inputwidth);
				$(this).parent().append('<input type="text" class="sample" style="width: '+ inputwidth +'px;" placeholder="Sample text..." name="sample['+inputid+']"/>');
				
			});

			if ($(".section").length > 0) {
				$("#default-section").css("display","none");	
			}

			if(sectionwidth < 1050) {
				$(".section").css("float","left");	
			}

		});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="new_section">
<div id="default-section">Default</div>
</div>

<div style="clear: both;"></div>

<button type="button" id="add_section">Add section</button>

最佳答案

一种方法是您可以添加一个id,例如@Pete答案

如果您不想添加id,则将$(".add_text").on("click")移到外部并与绑定(bind)#new_section。参见下面的代码:

$(".add_text").on("click", function() ); 移至外部并

$("#add_section").on("click", function() {
			
			var sectionid = $(".sekcja").length;
			$("#sectionid").val(sectionid);
			var sectionwidth = prompt("Section width");
			$("#sectionwidth").val(sectionwidth);
			var sectionheight = prompt("Section height");
			$("#sectionheight").val(sectionheight);
			var bg = prompt("Section background color");
			$("#bg").val(bg);
			var sectioncolor = prompt("Section font color");
			$("#sectioncolor").val(sectioncolor);
			$("#new_section").append('<div class="section" style="width: '+ sectionwidth +'px; min-height: '+ sectionheight +'px; background: #'+ bg +'; color: #'+ sectioncolor +';"><button type="button" class="add_text">Add text</button></div>');
			

			if ($(".section").length > 0) {
				$("#default-section").css("display","none");	
			}

			if(sectionwidth < 1050) {
				$(".section").css("float","left");	
			}

});

$("#new_section").on("click", ".add_text", function() {
				
	var inputid = $('.sample').length;
	$("#inputid").val(inputid);
	var inputwidth = prompt("Width text area");
	$("#inputwidth").val(inputwidth);
	$(this).parent().append('<input type="text" class="sample" style="width: '+ inputwidth +'px;" placeholder="Sample text..." name="sample['+inputid+']"/>');
				
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="new_section">
<div id="default-section">Default</div>
</div>

<div style="clear: both;"></div>

<button type="button" id="add_section">Add section</button>

关于javascript - 添加时重复输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45348789/

相关文章:

javascript - HTML5 中数字的自定义验证 CVC CVV

javascript - JQuery 选项卡不起作用 - 没有错误

php将一个包含一些js的网页包含到一个div中

javascript - 为所有主流浏览器创建 css3 无限旋转

javascript - 使用正则表达式从函数中删除参数类型

javascript - JQuery 切换器 - 单击一个切换器应更新另一个切换器

javascript - 自动完成后的事件填充 Asp.net C# 或 javascript/html,

javascript - 在 JavaScript 中测试两个值的相等性,其中内容包含 &

html - CSS 进度跟踪器未填满

css - 移动 View 中的 html5 有边框,但在 pc View 中没有