问题
I'm trying to learn about jquery as much as possible, looking at many examples around the web, but still got problem with events.
As you can see, I made a simple drag&drop thing using jquery UI, but all examples I found arround didn't help me with validating this.
I created correctAnswers but the problem is I have no idea how to validate them. As it's easy to see, 'a' goes to 1, 'b' goes to 2, and 'c' goes to 3, and when I click 'submit' button it validates.
var correctAnswers = ['a-1,b-2,c-3'];
$('.dragAble').draggable({
containment: "#content",
cursor: "move",
snap: ".dropAble"
});
if (answers === correctAnswers) {
$("#win").show();
} else {
$("#fail").show();
};
JSFiddle
回答1:
Try this code:
var correctAnswers = ['a-1', 'b-2', 'c-3'];
var results = [];
$('.dragAble').draggable({
containment: "#content",
cursor: "move",
snap: ".dropAble",
});
$( ".dropAble" ).droppable({
drop: function( event, ui ) {
results.push($(ui.draggable).attr("id") + '-' + $(this).attr("id"));
}
});
$("#submit").click(function(){
console.log(results);
result = true;
$.each(results, function(index, value){
if($.inArray(value, correctAnswers ) == -1){
result = false;
}
if(!result) return;
});
if (result) {
$("#win").show();
} else {
$("#fail").show();
};
});
Here the fiddle http://jsfiddle.net/aanred/x3jF3/. But this code has only basic function you need to improve it.
来源:https://stackoverflow.com/questions/13615567/jquery-validation-based-on-drag-dropped-items