Adding values from a radio button group
Hi to all in the forum!
I'm trying to develop a module for an aplication and i'm basing on an example from the jquery ui dialog
- http://jqueryui.com/demos/dialog/modal-form.html
But i'm using radio buttons and a datepicker field but problems occur when i try to add a second row to the table. It just adds the date value but the "motivo" value don't.
I mean everything works fine at the first attempt but at the next it fails.
Here is part of my code:
- $(function() {
var fecha = $("#datepicker"),
motivo = $("[name=motivo]"),
allFields = $([]).add(fecha).add(motivo),
tips = $("#validateTips");
function updateTips(t) {
tips.text(t).effect("highlight",{},1500);
}
function checkLength(o,n,min,max) {
if ( o.val().length > max || o.val().length < min ) {
o.addClass('ui-state-error');
updateTips("Debe seleccionar una fecha.");
return false;
} else {
return true;
}
}
function opcionSel(o,n) {
if( $("input[type='radio']:checked").length == 0){
$("#areaMotivo").addClass('ui-state-error');
updateTips("Debe seleccionar un motivo.");
return false;
}
else{
return true;
}
}
$("#dialog").dialog({
bgiframe: true,
autoOpen: false,
height: 300,
modal: true,
buttons: {
'Agregar día': function() {
var bValid = true;
allFields.removeClass('ui-state-error');
bValid = bValid && checkLength(fecha,"fecha",10,10);
bValid = bValid && opcionSel(motivo,"motivo");
if (bValid) {
$('#users tbody').append('<tr>' +
'<td>' + fecha.val() + '</td>' +
'<td>' + motivo.val() + '</td>' +
'</tr>');
$(this).dialog('close');
}
},
Cancelar: function() {
$(this).dialog('close');
}
},
close: function() {
allFields.val('').removeClass('ui-state-error');
}
});
$('#create-user').click(function() {
$('#dialog').dialog('open');
})
.hover(
function(){
$(this).addClass("ui-state-hover");
},
function(){
$(this).removeClass("ui-state-hover");
}
).mousedown(function(){
$(this).addClass("ui-state-active");
})
.mouseup(function(){
$(this).removeClass("ui-state-active");
});
$(function() {
$("#datepicker").datepicker({ dateFormat: 'yy-mm-dd'});
});
});
</script>
I think the problem could be because i'm using a radio group with this function
- function opcionSel(o,n) {
if( $("input[type='radio']:checked").length == 0){
$("#areaMotivo").addClass('ui-state-error');
updateTips("Debe seleccionar un motivo.");
return false;
}
else{
return true;
}
}
And here too, the values that motivo.val has are [input#motivo1, input#motivo2, input#motivo3] when using firebug.
- if (bValid) {
$('#users tbody').append('<tr>' +
'<td>' + fecha.val() + '</td>' +
'<td>' + motivo.val() + '</td>' +
'</tr>');
$(this).dialog('close');
}
But i don't know why works only one time.
Colud someone help me?