问题
I am currently working with Jquery UI tabs that I have only operating with next/previous buttons(this will force the user to go through the tabs in order). In my first tab I have a set of dynamic select boxes and I am trying to get the user to choose a category and its subcategory from that corresponds before given the possibility to enable the NEXT button. But the next button is not being disabled at all.
How can I get the user to choose from all the corresponding select boxes and then enabling the NEXT button? EXAMPLE
I added $('.update').change(function() to the JS for NEXT/PREVIOUS to detect the selected item in the last select box and give the go for the NEXT Button
JS for NEXT/Previous
<script>
$(function() {
var $tabs = $('#tabs').tabs({
disabled: [0, 1]
});
$(".ui-tabs-panel").each(function(i) {
var totalSize = $(".ui-tabs-panel").size() - 1;
if (i != totalSize) {
next = i + 2;
$(this).append("<a href='#' class='next-tab mover' rel='" + next + "'>Next Page »</a>");
}
if (i != 0) {
prev = i;
$(this).append("<a href='#' class='prev-tab mover' rel='" + prev + "'>« Prev Page</a>");
}
});
$('.update').change(function() {
$('#tabs').tabs(
'option',
'disabled',
(
$(this).hasClass('last') &&
parseInt($(this).val(), 10) > 0 ?
[] :
[1]
)
);
});
$('.next-tab, .prev-tab').click(function() {
var tabIndex = $(this).attr("rel");
$tabs.tabs('enable', tabIndex)
.tabs('select', tabIndex)
.tabs("option","disabled", [0, 1]);
return false;
});
});
</script>
upadateSelectBox.js
// following line is added to detect last select box picked
$('.update').removeClass('last');
if (!data.error) {
obj.next('.update').html(data.list).removeAttr('disabled hidden');
} else {
// following line is changed
obj.addClass('last').nextAll('.update').attr({'disabled': true, 'hidden':true}).html('<option value="">----</option>');
}
HTML of select boxes
<select name="gender" id="gender" class="update" size="7">
<option value="">Select one</option>
<?php if (!empty($list)) { ?>
<?php foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<?php echo $row['category_name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
<select name="category" id="category" class="update" disabled="disabled" hidden="hidden" size="7">
<option value="">----</option>
</select>
<select name="colour" id="colour" class="update" disabled="disabled" hidden="hidden" size="7">
<option value="">----</option>
</select>
回答1:
Ok, this is untested. Only change the tab on next/previous click, if you are not on the first tab (1) or the really last tab exist and an option (except the neutral) is choosen (2).
$('.next-tab, .prev-tab').click(function() {
var tabIndex = $(this).attr("rel");
if (
/*(1)*/ $('#tabs').tabs('option', 'selected') > 0 ||
/*(2)*/ ($('.update.last').length > 0 && parseInt($('.update.last').val(), 10) > 0)
) {
$tabs.tabs('enable', tabIndex)
.tabs('select', tabIndex)
.tabs("option","disabled", [0, 1]);
}
return false;
});
Good luck and see you tomorrow :-)
来源:https://stackoverflow.com/questions/11674461/jquery-ui-tabs-next-and-previous-enable-disable-based-on-select-boxes