I have a html page with a dynamic section that changes depending on the options selected. Here´s options part:
<div class="form-group-lg">
@foreach (var respuesta in Model.ListRespuestas) {
<div class="col-md-8">
<label class="control-label" for="@respuesta.Id">
@respuesta.Nombre
</label>
<input type="radio" name="SelectedItem" id="@respuesta.Id" value="@respuesta.Id" style="margin-right: 10px;" />
</div>
}
</div>
This changes when the "next" ("siguiente" for non spanish speakers) button is pressed. This is the code for the button:
<div class="col-md-6 text-right">
<span data-placement="top" data-toggle="tooltip" title="" data-original-title="Confirmación de acción">
<input type="submit" id="siguiente" class="btn btn-primary" value="Siguiente" />
</span>
</div>
And this button is scripted here:
<script>
$('#siguiente').click(function (e) {
e.preventDefault();
SendAjaxForm();
});
$('input:radio[name="SelectedItem"]').change(
function () {
if (this.checked) {
$.post('@Url.Content("~/Home/Opciones/?idrespuesta=")' + this.id, function (data) {
$("#content-options").html(data);
});
}
});
</script>
All of this works fine. The page displays the first question, then the user chooses one and clicks next, then the frame changes and displays the correspondent next questions and the process goes on until it finishes the question branch.
However, I can't work out how to code the button to throw a warning message when clicked and no option is selected (the tricky part is the none selected behavior, I know how to throw exceptions). Any help (including docs or wikis) are welcome. Thanks everyone in advance.
EDIT - UPDATE
So I've updated the button script
<script>
$('#siguiente').click(function (e) {
if ($('input:radio[name="SelectedItem"]').is(':checked')){
e.preventDefault();
SendAjaxForm();
$('input:radio[name="SelectedItem"]').change(function () {
if (this.checked) {
$.post('@Url.Content("~/Home/Opciones/?idrespuesta=")' + this.id, function (data) {
$("#content-options").html(data);
});
}
});
} else {
alert("No se ha seleccionado nada");
}
});
</script>
And now I show an alert message when no option is selected, but the "SendAjaxForm" function still gets executed - generating an exception (reference to null parameter while expecting int because the field "idrespuesta" is null, no option selected). Don't know why. Anyway thanks everyone.