我正在Wordpress上的这个项目上工作,我需要使用append和删除动态更改HTML中的形式。我尝试过使用它,但有一些错误。它不按照它应该的方式工作。这是代码的简要说明:如何使用append和remove来更改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星评级正常工作,即。正确添加和删除。
预先感谢。
第一张图片显示的第一输出增加2输入表格和5星级评分之前:
第二个图像显示按下“添加新字段+”的第2输入表格后删除按钮后和5星级评分:
谢谢,它的工作现在。 – codewiz
@codewiz welcome :) – Roljhon
如果可能,您是否可以提供任何资源以了解有关此主题的更多信息?谢谢。 – codewiz