我正在 WordPress 上开发这个项目,我需要使用追加和删除来动态更改 html 中的表单。我尝试使用它,但有一些错误。它没有按应有的方式工作。这是代码的简要说明:
<?php
/* Template Name: FirstStarRatingSystem */
?>
<div class="t1">
<button class="add_form_field">Add New Field <span style="font-
size:16px; font-weight:bold;">+ </span></button><br>
<input type="text" class="t1" name="fname1" id="text1" placeholder="First
Rating"></div>
<div class="rating1">
<legend>Please rate:</legend>
<input type="radio" class="rating1" id="star5" name="rating1" value="5" />
<label for="star5" title="Rocks!">5 stars</label>
<input type="radio" class="rating1" id="star4" name="rating1" value="4" />
<label for="star4" title="Pretty good">4 stars</label>
<input type="radio" class="rating1" id="star3" name="rating1" value="3" />
<label for="star3" title="Meh">3 stars</label>
<input type="radio" class="rating1" id="star2" name="rating1" value="2" />
<label for="star2" title="Kinda bad">2 stars</label>
<input type="radio" class="rating1" id="star1" name="rating1" value="1" />
<label for="star1" title="Sucks big time">1 star</label>
</div>
<br><br>
<br><br>
<br><br>
<div class="t2"></div>
<div class="rating2"></div>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
$(document).ready(function() {
var max_fields=2;
var t2=$(".t2");
var rating2=$(".rating2");
var add_button=$(".add_form_field");
var x=1;
$(add_button).click(function(e){
e.preventDefault();
if(x < max_fields){
x++;
if(x==2)
$(t2).append('<div class="t2"><input type="text" class="t2" name="fname2"
id="text2" placeholder="Second Rating"></div>'); //add input box
$(rating2).append('<div class="rating2"><legend>Please rate1:</legend>
<input type="radio" class="rating2" id="1star5" name="rating2" value="5" />
<label for="1star5" title="Rocks!">5 stars</label> <input type="radio"
class="rating2" id="1star4" name="rating2" value="4" /> <label for="1star4"
title="Pretty good">4 stars</label> <input type="radio" class="rating2"
id="1star3" name="rating2" value="3" /> <label for="1star3" title="Meh">3
stars</label> <input type="radio" class="rating2" id="1star2" name="rating2"
value="2" /> <label for="1star2" title="Kinda bad">2 stars</label> <input
type="radio" class="rating2" id="1star1" name="rating2" value="1" /> <label
for="1star1" title="Sucks big time">1 star</label><a href="#"
class="delete">Delete</a></div>'); //add 5 star rating.
}
else
{
alert('You Reached the limits')
}
});
$(rating2).on("click",".delete", function(e){
e.preventDefault();
$(t2).remove();
$(this).parent('div').remove();
x--;
})
});
</script>
<style>NECESSARY CSS</style>
正如您所想,此代码用于显示动态输入表单以及 5 星级评级系统。用户可以选择输入表格的数量和所需的 5 星级评级。目前,出于测试目的,此数字仅限 2 次 (var max_fields = 2;)。我需要在用户按下添加按钮的同时添加输入表单和 5 星级评级,并在用户按下删除按钮时同时删除输入表单和 5 星级评级,但目前这无法正常工作并导致第二次添加输入表单将被永久删除,只有 5 星级评级才能正常工作,即。正确添加和删除。
提前致谢。
第一张图片显示添加第二个输入表单和 5 星级评级之前的第一个输出:
删除第二个输入表单和 5 星评级后按“添加新字段 +”按钮后显示第二张图片:
最佳答案
您的问题实际上是因为您的输入字段的容器具有相同的类名,即 t2
,因此通过下面的内容,您将删除两者(容器和输入字段以及定义的 t2 HTML 中的元素)。
$(rating2).on("click",".delete", function(e){
e.preventDefault();
$(t2).remove();
$(this).parent('div').remove();
x--;
})
因此,当您追加时,DOM 中不再提供 t2
容器。
因此 var t2
引用 null 值,因此 append
将不起作用。
你应该做这样的事情。
$(rating2).on("click",".delete", function(e){
e.preventDefault();
$(t2).find('input').remove(); // find the input element and remove
$(this).parent('div').remove();
x--;
})
或者将您的输入字段类更改为与您的容器不相似的内容
您的声明
var t2=$(".t2");
如何附加
$(t2).append('<div class="t2"><input type="text" class="t2" name="fname2" id="text2" placeholder="Second Rating"></div>');
如何删除
$(t2).remove();
关于javascript - 如何使用追加和删除来更改 html 表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43298850/