0

我正在尝试从以下代码创建一个表单,但没有任何成功。我想输入隐藏字段以在我将可拖动项目之一放入“最喜欢我”字段时捕获值 1,当可拖动项目在“第二个最喜欢我”时,值为 2,3 表示“ “最喜欢我”第三名,“最不喜欢我”第四名。样品可以点击这里查看。我将如何使用以下代码创建它?我使用 PHP 和 MySQL 作为添加到数据库的参考。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
/* Add some margin to the page and set a default font and colour */

body {margin: 30px;font-family: "Georgia", serif;line-height: 1.8em;color: #333;}

/* Give headings their own font */

h1, h2, h3, h4 {
  font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}

/* Main content area */

#content {
  margin: 80px 70px;
  text-align: center;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
}

/* Header/footer boxes */

.wideBox {
  clear: both;
  text-align: center;
  margin: 70px;
  padding: 10px;
  background: #ebedf2;
  border: 1px solid #333;
}

.wideBox h1 {
  font-weight: bold;
  margin: 20px;
  color: #666;
  font-size: 1.5em;
}

/* Slots for final card positions */

#cardSlots {
  margin: 50px auto 0 auto;
  background: #ddf;
}

/* The initial pile of unsorted cards */

#cardPile {
  margin: 0 auto;
  background: #ffd;
}

#cardSlots, #cardPile {
  width: 910px;
  height: 120px;
  padding: 20px;
  border: 2px solid #333;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  -moz-box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
  -webkit-box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
  box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
}

/* Individual cards and slots */

#cardSlots div, #cardPile div {
  float: left;
  width: 150px;
  height: 78px;
  padding: 10px;
  padding-top: 40px;
  padding-bottom: 0;
  border: 2px solid #333;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  margin: 0 0 0 10px;
  background: #fff;
}

#cardSlots div:first-child, #cardPile div:first-child {
  margin-left: 0;
}

#cardSlots div.hovered {
  background: #aaa;
}

#cardSlots div {
  border-style: dashed;
}

#cardPile div {
  background: #666;
  color: #fff;
  font-size: 20px;
  text-shadow: 0 0 3px #000;
}

#cardPile div.ui-draggable-dragging {
  -moz-box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
  -webkit-box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
  box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
}

</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>

<script type="text/javascript">

// JavaScript will go here

$( init );

function init() {

  // Create the pile of shuffled cards
  {
    $('#cardPile div').draggable( {
      containment: '#content',
      stack: '#cardPile div',
      cursor: 'move',
      revert: true
    } );
  }

  // Create the card slots
  {
    $('#cardSlots div').droppable( {
      accept: '#cardPile div',
      hoverClass: 'hovered',
      drop: handleCardDrop
    } );
  }

function handleCardDrop( event, ui ) {
    var slotNumber = $(this);
    var cardNumber = ui.draggable;

    if ( cardNumber == cardNumber ) {
        ui.draggable.addClass ( 'correct' );
        ui.draggable.draggable ( 'disable' );
        $(this).droppable( 'disable' );
        ui.draggable.position( {of: $(this), my: 'left top', at: 'left top' } );
        ui.draggable.draggable( 'option', 'revert', false );
    }
}



}

</script>
</head>

<body>

<div id="content">
  <div id="cardPile">
  <div>Controlling</div>
  <div>Motivating</div>
  <div>Realistic</div>
  <div>Organized</div>
  </div>

  <div id="cardSlots">
  <div>Most Like Me</div>
  <div>2nd Most Like Me</div>
  <div>3rd Most Like Me</div>
  <div>Least Like Me</div>
  </div>

</div>

</body>
</html>
4

1 回答 1

0

我修改了你的功能:

function handleCardDrop( event, ui ) {
var slotNumber = $(this);
var cardNumber = ui.draggable;
var choice = cardNumber.html();
var aux = 0;
if (choice == 'Controlling')
{
    aux = 1;
}
if (choice == 'Motivating')
{
    aux = 2;
}
if (choice == 'Realistic')
{
    aux = 3;
}
if (choice == 'Organized')
{
    aux = 4;
}
var droped= slotNumber.html();

if(droped == 'Most Like Me')
{
    $("#uno").val(aux);
}
if(droped == '2nd Most Like Me')
{
    $("#dos").val(aux);
}
if(droped == '3rd Most Like Me')
{
    $("#tres").val(aux);
}
if(droped == 'Least Like Me')
{
    $("#cuatro").val(aux);
}
if ( cardNumber == cardNumber ) {
    ui.draggable.addClass ( 'correct' );
    ui.draggable.draggable ( 'disable' );
    $(this).droppable( 'disable' );
    ui.draggable.position( {of: $(this), my: 'left top', at: 'left top' } );
    ui.draggable.draggable( 'option', 'revert', false );
}
}

------------------更新2.0---------------

我只是添加了 ajax 部分,并修改了小提琴:

  $("#saveResult").click(function(){

var uno = $("#uno").val();  
var dos = $("#dos").val();  
var tres = $("#tres").val();  
var cuatro = $("#cuatro").val();
var params = {

            "uno" : uno,

            "dos" : dos,

            "tres" : tres,

            "cuatro": cuatro

    };    
 $.ajax( {
  type: "POST",
  url: 'myphpfile.php',
  data: params,
  success: function( response ) {
    $("#result").html(response);
  }
} );

}); 

在文件 myphpfile.php 中:

<?php
$uno = $_POST['uno'];
$dos = $_POST['dos'];
$tres = $_POST['tres'];
$cuatro = $_POST['cuatro'];

//Here the code to insert in your database

?>

更新的工作小提琴:http: //jsfiddle.net/robertrozas/qLhke/25/

于 2013-11-07T01:39:48.153 回答