试图使可拖动的表单元素水平jQuery
Trying to make draggable form elements horizontal jQuery
有人能告诉我如何在不失去功能的情况下使可拖动图像看起来水平吗。如果我将样式设置为水平,则图像将不再可拖动。我正在使用jQuery&jQueryUI来实现表单.
JQuery
<script type='text/javascript'>
$(window).load(function(){
$(function() {
$( "#sortable" ).sortable({
placeholder: "ui-state-highlight",
cursor: 'crosshair',
update: function(event, ui) {
var order = $("#sortable").sortable("toArray");
order = JSON.stringify(order);
correct = JSON.stringify(["4","3","2","1"]);
if (!order==correct){
var valid = false
console.log(':(');
}
else {
var valid = true;
}
}
});
$( "#sortable" ).disableSelection();
$('#submit').on('click', function() {
var valid = true,
message = '';
$('form input').each(function() {
var $this = $(this);
if(!$this.val()) {
var inputName = $this.attr('name');
valid = false;
message += 'Please enter your ' + inputName + ''n';
}
});
if(!valid) {
alert(message);
}
else {
window.location.href = "http://stackoverflow.com";
}
});
});
});
</script>
Html表单
<body>
<form action="" method="post" class="a">
Name : <input type="text" class="text" name="name" id="name" /> <br/>
Address : <input type="text" class="text" name="address" id="address" /> <br/>
email : <input type="text" class="text" name="email" id="email" /> <br/>
<input type="hidden" id="image_order" name="image_order" value="order" />
<ul id="sortable" style="width: 524px;">
<li id="1" class="ui-state-default"><img src="1.png" width="100" height="90" /></li>
<li id="2" class="ui-state-default"><img src="2.png" width="100" height="90" /></li>
<li id="3" class="ui-state-default"><img src="3.png" width="100" height="90" /></li>
<li id="4" class="ui-state-default"><img src="4.png" width="100" height="90" /></li>
</ul>
<div style="clear:both;"></div>
<input name="Submit" value="RE-ORDER" type="submit" />
<input type="button" id="submit" value="Submit" name="submit" />
</form>
</body>
您只需要像这个一样将样式display: inline-block
添加到li
元素中
li {
display: inline-block;
margin: 10px; // This just to give some space between the image
}
当拖动一个图像时,另一个图像将占据剩余的空间,如果您希望拖动的图像保持空间,请将width
赋予li
,使其与您的image width
相同(如果所有图像都具有相同的宽度)。
li {
display: inline-block;
margin: 10px; // This just to give some space between the image
width: 100px; // To reserve the space
}
这是正在运行的演示
编辑:
为了在评论中回答更多关于你的问题,导致你的验证有效的主要问题是因为你在sortable update
和submit onclick
中使用了相同的变量valid
和individual,因此如果你已经填写了所有的输入字段,就会导致你的有效性验证,要解决这个问题,你需要在开始时声明他们自己的变量,并通过验证更改其值,更改后的代码变成这样:
$(function () {
var validInput, validSort; // Added this line
$("#sortable").sortable({
placeholder: "ui-state-highlight",
cursor: 'crosshair',
update: function (event, ui) {
var order = $("#sortable").sortable("toArray");
order = JSON.stringify(order);
correct = JSON.stringify(["4", "3", "2", "1"]);
if (order == correct) {
validSort = true // Changed this line
console.log(':)');
}
else {
validSort = false; // Added this line
}
}
});
$("#sortable").disableSelection();
$('#submit').on('click', function () {
validInput = true, // Changed this line
message = '';
$('form input').each(function () {
var $this = $(this);
if (!$this.val()) {
var inputName = $this.attr('name');
validInput = false; // Changed this line
message += 'Please enter your ' + inputName + ''n';
}
});
if (!validInput) {
alert(message);
console.log("not valid");
} else if(!validSort) { // Added this line
alert("Sort Not Valid");
//window.location.href = "http://stackoverflow.com";
console.log("not valid");
}
else {
alert("Valid Submit");
console.log("valid");
}
return false;
});
});
这是正在运行的演示
相关文章:
- Javascript更新孙窗口中的表单元素
- 阻止表单元素提交
- 使用带有.net autopostback的ryanfait.com自定义表单元素
- 使用javascript更改表单元素的ID值
- HTML5在提交并显示所需标签后显示隐藏的表单元素
- 为什么表单元素不应命名为submit
- 如何重置搜索表单中的特定表单元素
- 使用 ng-repeat访问 ng 表单元素/值
- 获取电子邮件附件的表单元素
- AJAX和php脚本后添加的表单元素未传输到$_POST
- 如何对未知表单元素进行表单验证
- 不显示为表单元素的图像输入按钮的动态创建和appendChild
- 在JavaScript中为表单元素生成键值对
- 表单元素上的jQuery focusout事件即使在单击子元素时也会被触发
- 如何在 OnKeyPress 事件后获取输入表单元素的值
- 表单元素值更改后的角度 JS 验证
- 使用 javascript 中的表单元素构建一个 xml 字符串
- 如何以角度访问表单元素的$valid
- 如果选择了多个选项,则使用 jQuery 显示隐藏的表单元素
- JSON 填充的表单元素不使用 angularJs 过滤器进行过滤