我正在尝试创建一个html表单元素,使用户可以按顺序放置四个东西。例如,我想问一个用户他们最喜欢的动物是什么,并按顺序排列。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script>
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
updateOrder(ev.target.id);
}
function updateOrder(id) {
var w = document.getElementById(id);
for (var i = 0; i < w.childNodes.length; i++) {
var node = w.childNodes[i];
document.getElementById('order-'+i).value=node.id;
}
}
</script>
</head>
<body>
<div class="container">
<h2>Vertical (basic) form</h2>
<form role="form">
<div class="form-group">
<label for="order">Put in order:</label>
<input type="hidden" name="order-1" id="order-0" value="">
<input type="hidden" name="order-2" id="order-1" value="">
<input type="hidden" name="order-3" id="order-2" value="">
<input type="hidden" name="order-4" id="order-3" value="">
<div style="border: 1px dashed #ddd; padding: 0.5em; min-height: 2em; margin-bottom: 4px;" ondrop="drop(event)" ondragover="allowDrop(event)" id="drop-from" placeholder="Pak hier de opties">
<div draggable="true" ondragstart="drag(event)" style="float: left;" id="option-1" class="label label-default">Optie 1</div>
<div draggable="true" ondragstart="drag(event)" style="float: left;" id="option-2" class="label label-default">Optie 2</div>
<div draggable="true" ondragstart="drag(event)" style="float: left;" id="option-3" class="label label-default">Optie 3</div>
<div draggable="true" ondragstart="drag(event)" style="float: left;" id="option-4" class="label label-default">Optie 4</div>
</div>
<div class="form-control" ondrop="drop(event)" ondragover="allowDrop(event)" id="drop-form" placeholder="sleep ze in de juiste volgorde hier naartoe"></div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</body>
</html>
现在我的问题是,当我想重新排列掉落的元素时,尤其是当您在#option 2上删除#option-1时,我有意想不到的输出。而不是在#option-2之后移动#option-1,它将#option-1放在#option-2中,隐藏的输入字段变为奇怪的值。有人可以帮帮我吗?
答案 0 :(得分:0)
您的代码存在一些问题。请查看它。
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
var dropFrom = document.getElementById('drop-from');
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
if (ev.target === dropFrom) {
ev.target.appendChild(document.getElementById(data));
}
else {
ev.target.parentNode.insertBefore(document.getElementById(data), ev.target.nextSibling)
}
updateOrder(ev.target.id);
}
function updateOrder(id) {
var w = document.getElementById('drop-from');
var children = w.getElementsByTagName('div');
for (var i = 0; i < children.length; i++) {
var node = children[i];
document.getElementById('order-'+i).value=node.id;
}
}
&#13;
<form role="form">
<div class="form-group">
<label for="order">Choose your favorite animals and put it in order:</label>
<input type="hidden" name="order-1" id="order-0" value="">
<input type="hidden" name="order-2" id="order-1" value="">
<input type="hidden" name="order-3" id="order-2" value="">
<input type="hidden" name="order-4" id="order-3" value="">
<div style="border: 1px dashed #ddd; padding: 0.5em; min-height: 2em; margin-bottom: 4px;" ondrop="drop(event)" ondragover="allowDrop(event)" id="drop-from" placeholder="Pak hier de opties">
<div draggable="true" ondragstart="drag(event)" style="float: left;" id="option-1" class="label label-default">Horse</div>
<div draggable="true" ondragstart="drag(event)" style="float: left;" id="option-2" class="label label-default">Dog</div>
<div draggable="true" ondragstart="drag(event)" style="float: left;" id="option-3" class="label label-default">Cat</div>
<div draggable="true" ondragstart="drag(event)" style="float: left;" id="option-4" class="label label-default">Cow</div>
</div>
<div class="form-control" ondrop="drop(event)" ondragover="allowDrop(event)" id="drop-form" placeholder="sleep ze in de juiste volgorde hier naartoe"></div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
&#13;
答案 1 :(得分:0)
Fragment f = getActivity().getSupportFragmentManager().findFragmentByTag("Fragment_tag");
if (f instanceof FragmentName) {
if (f != null)
getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit()
}