因此,我的网站上有一个按钮,让人们可以将游戏类别添加到一个配置文件中,除非在下拉列表中选择某些内容,否则我无法单击"添加到配置文件"。我尝试了一些事情,但是我看到了我想要的结果,这是代码的表格块:
<div class="form-group">
<label for="" class="col-md-3 control-label">Game Categories *</label>
<div class="col-md-9">
<select name="game_categories_id" id="game_categories_id" class="form-control">
<option value="0">Select One...</option>
<?php foreach ($game_categories as $game_category): ?>
<option value="<?php echo $game_category->ID; ?>">
<?php echo $game_category->name; ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
在我的提交区域中,我有以下内容:
<div class="modal-footer">
<?php if (!empty($_POST['game_categories_id']) && ($_POST['game_categories_id'] == 0)) { ?>
<button type="button" class="btn btn-primary" id="save_game_categories_id" ?>">Add Game</button>
<?php } elseif (!empty($_POST['credit_reporting_org_id']) && ($_POST['credit_reporting_org_id'] != 0)) { ?>
<button type="button" class="btn btn-primary" disabled id="save_game_categories_id" ?>">Add Game</button>
<?php } ?>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
我想发生的事情是,在下拉列表中选择某些内容之前,添加游戏按钮是不可单击的,但是现在除了关闭按钮以外没有显示任何内容。
也有一个JS文件,该文件涉及添加游戏类别:
// Save game categories
$('#modal').on('click', '#save_game_categories_id', function() {
var errors = 0;
var $form = $('#add_game_categories_form');
// Make sure game fields are filled
$.each([$('#game_categories_id'), $('#game_type_id')], function(i, $input) {
if ($input.val() == '' || $input.val() == 0) {
$input.parents('.form-group').addClass('has-error');
errors++;
} else {
$input.parents('.form-group').removeClass('has-error');
}
});
if (errors > 0) return false; // Don't proceed if there are errors
$.post('/game/approved_game/' + game_topic_id, $form.serialize(), function(
data
) {
if (data.success == true) {
refresh_approved_game();
$('#modal').modal('hide');
} else {
$form.find('.alert').show();
}
});
});
所以我已经有了JS,我知道它可以添加游戏,但是如果他们在下拉列表中选择任何内容
您可能想使用一些JavaScript完成此操作。您可以使用这样的jQuery:
$('#game_categories_id').on('change', function(){
var noAnswer = ($(this).val() == 0);
$('#save_game_categories_id').attr('disabled', noAnswer);
});
如果您需要使用香草JS,我也可以在其中添加一个解决方案。
this:
<?php if (!empty($_POST['game_categories_id']) && ($_POST['game_categories_id'] == 0)) { ?>
应该是:
<?php if (!empty($_POST['game_categories_id']) && ($_POST['game_categories_id'] != 0)) { ?>
,当选择某物时,用户会添加游戏。
据我了解,下面的附加示例代码:
<button type="button" class="btn btn-primary" <?php if(userSelectedCategory == ''){ echo'disabled';} ?> id="save_game_categories_id" ?>">Add Game</button>
如果不选择任何类别的其他明智删除 disabled属性,这将禁用您的按钮,这意味着添加游戏按钮是已启用