該腳本的構思是它爲它們添加了部分和文本框。如果我添加一個部分並添加輸入,則所有內容都會播放。但是,當我添加另一個 - 在其中一個提示重複,我不明白爲什麼,我把代碼來檢查我的意思。添加時重複輸入
$("#add_section").on("click", function() {
\t \t \t
\t \t \t var sectionid = $(".sekcja").length;
\t \t \t $("#sectionid").val(sectionid);
\t \t \t var sectionwidth = prompt("Section width");
\t \t \t $("#sectionwidth").val(sectionwidth);
\t \t \t var sectionheight = prompt("Section height");
\t \t \t $("#sectionheight").val(sectionheight);
\t \t \t var bg = prompt("Section background color");
\t \t \t $("#bg").val(bg);
\t \t \t var sectioncolor = prompt("Section font color");
\t \t \t $("#sectioncolor").val(sectioncolor);
\t \t \t $("#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>');
\t \t \t $(".add_text").on("click", function() {
\t \t \t \t
\t \t \t \t var inputid = $('.sample').length;
\t \t \t \t $("#inputid").val(inputid);
\t \t \t \t var inputwidth = prompt("Width text area");
\t \t \t \t $("#inputwidth").val(inputwidth);
\t \t \t \t $(this).parent().append('<input type="text" class="sample" style="width: '+ inputwidth +'px;" placeholder="Sample text..." name="sample['+inputid+']"/>');
\t \t \t \t
\t \t \t });
\t \t \t if ($(".section").length > 0) {
\t \t \t \t $("#default-section").css("display","none"); \t
\t \t \t }
\t \t \t if(sectionwidth < 1050) {
\t \t \t \t $(".section").css("float","left"); \t
\t \t \t }
\t \t });
<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>
它看起來像一個'click'事件處理程序附加到'.add_text'按鈕(一個或多個)每有人點擊時「添加部分」按鈕。你應該從部分點擊處理程序中移除(「click」,function(){...}'部分的'$(「。add_text」)。 – matov