我正在尝试创建一个允许用户编辑它的表单,类似于旧式的 Facebook 个人资料编辑。我不知道如何同时更改一个部分中的所有元素。我可以一次编辑一个,但不是全部。
HTML:
<div id="wrapper">
<section id="core">
<div class="profileinfo">
<div class="gear">
<label>Primary E-Mail:</label><br />
<span id="pemail" class="datainfo">myaddress@googlemail.com</span>
<a href="#" class="editlink">Edit Info</a>
<a class="savebtn">Save</a>
</div>
<div class="gear">
<label>Full Name:</label><br />
<span id="first" class="datainfo">Johnny</span>
<span id="middle" class="datainfo">James</span>
<span id="last" class="datainfo">Appleseed</span>
<a href="#" class="editlink">Edit Info</a>
<a class="savebtn">Save</a>
</div>
<div class="gear">
<label>Birthday:</label><br />
<span id="birthday" class="datainfo">August 21, 1989</span>
<a href="#" class="editlink">Edit Info</a>
<a class="savebtn">Save</a>
</div>
<div class="gear">
<label>Address:</label><br />
<span id="address1" class="datainfo">123 Test Lane</span><br />
<span id="address2" class="datainfo">APT 104</span><br />
<span id="city" class="datainfo">Anywhere</span>
<span id="state" class="datainfo">FL</span>
<span id="zip" class="datainfo">55555</span>
<a href="#" class="editlink">Edit Info</a>
<a class="savebtn">Save</a>
</div>
<div class="gear">
<label>Occupation:</label><br />
<span id="occupation" class="datainfo">Freelance Web Developer</span>
<a href="#" class="editlink">Edit Info</a>
<a class="savebtn">Save</a>
</div>
</div>
</section>
</div>
Javascript:
$(document).ready(function(){
$(".editlink").on("click", function(e){
e.preventDefault();
var dataset = $(this).prev(".datainfo");
var savebtn = $(this).next(".savebtn");
var theid = dataset.attr("id");
var newid = theid+"-form";
var currval = dataset.text();
dataset.empty();
$('<input type="text" name="'+newid+'" id="'+newid+'" value="'+currval+'" class="hlite">').appendTo(dataset);
$(this).css("display", "none");
savebtn.css("display", "block");
});
$(".savebtn").on("click", function(e){
e.preventDefault();
var elink = $(this).prev(".editlink");
var dataset = elink.prev(".datainfo");
var newid = dataset.attr("id");
var cinput = "#"+newid+"-form";
var einput = $(cinput);
var newval = einput.attr("value");
$(this).css("display", "none");
einput.remove();
dataset.html(newval);
elink.css("display", "block");
});
});
编辑
这是一个类似于我希望它看起来像的图像:
如何建立编辑每个部分的能力?这是一个JSFiddle供您查看。