jquery - 我想在列表框中未选择任何内容时禁用按钮

标签 jquery html css

您好专家,我是 jQuery 的新手。如果从以上三个框中选择了任意三个值(每个框一个值),我已启用“添加类别”按钮。我只想在第四个框中有任何值时启用“删除类别”和“保存类别”按钮但是,我的代码不适用于此。

var one = $('.select-manage-category').val();
var two = $('.select-manage-category1').val();
var three = $('.select-manage-category2').val();


$('.select-manage-category, .select-manage-category1, .select-manage-category2').change(function() {
  var one = $('.select-manage-category').val();
  var two = $('.select-manage-category1').val();
  var three = $('.select-manage-category2').val();
  if (one && two && three) {
    $("#add-category").prop("disabled", false);
  } else {
    $("#add-category").prop("disabled", true);
  }

});

$('#selected-lst-values').change(function() {
  if ($(this).val()) {
    $('#remove-category').prop("disabled", false);
    $('#save-categories').prop("disabled", false);
  } else {
    $('#remove-category').prop("disabled", true);
    $('#save-categories').prop("disabled", true);
  }
});

$('#add-category').click(function() {
  $(
    '.select-manage-category, .select-manage-category1, .select-manage-category2'
  ).each(function() {
    $('#selected-lst-values').append('<option value="' + $(this).val() + '">' + $(this).val() + '</option>');
  });
});
$('#remove-category').click(function() { // check change here
  var select = document.getElementById('selected-lst-values');

  for (var i = 0; i < 3; i++) {
    select.removeChild(select.lastChild);
  }
    if ($('#selected-lst-values').val()) {
    $('#remove-category').prop("disabled", false);
    $('#save-categories').prop("disabled", false);
  } else {
    $('#remove-category').prop("disabled", true);
    $('#save-categories').prop("disabled", true);
  }



});
$('#selected-lst-values>option').on("change", function() {
  if ($(this).val() === '') {
    $('#mnage-category-savebtn').attr({
      disabled: disabled
    });

  } else {
    $('#mnage-category-savebtn').removeAttr('disabled');
  }
});
.select-manage-category,
.select-manage-category1,
.select-manage-category2 {
  width: 100px;
  float: left;
  margin-right: 4px;
}

p {
  clear: left;
  text-align: center;
}

#selected-lst-values {
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div><select class="form-control select-manage-category" size="5">
    <option>1</option>
    <option>2</option>
    <option>1</option>
    <option>2</option>
</select></div>

<div><select class="form-control select-manage-category1" size="5">
    <option>1</option>
    <option>2</option>
    <option>1</option>
    <option>2</option>
		</select></div>
<div><select class="form-control select-manage-category2" size="5">
    <option>1</option>
    <option>2</option>
    <option>1</option>
    <option>2</option>
		</select>
</div>
<p class="text-center color-red">You can add up to 20 categories</p>
</div>
<input id="add-category" name="add" type="button" value="Add Category" disabled>
<input id="remove-category" name="add" type="button" value="Remove Category" disabled>
<div><select id="selected-lst-values" class="form-group percent-100" size="5">
		</select></div>
<button id='save-categories' class="btn btn-md btn-radius pi-btn prodetails-btn" type="button" disabled><strong>Save Categories</strong> 
    <span class="glyphicon glyphicon-menu-right right-arrow-head-icon"></span>
</button>

最佳答案

尝试

$(document).ready(function(){
  if ($('#selected-lst-values').val() != null) {
    $('#remove-category').prop("disabled", false);
    $('#save-categories').prop("disabled", false);
  } else {
    $('#remove-category').prop("disabled", true);
    $('#save-categories').prop("disabled", true);
  }
});

关于jquery - 我想在列表框中未选择任何内容时禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42778629/

相关文章:

javascript - jQuery - 输入文本属性未显示在页面上

javascript - jquery如何在执行ajax调用后获取点击事件的索引

javascript - jQuery/jQuery Mobile - .append 后刷新页面格式

java - 从java中的String中删除Html标签,除了少数特定标签

javascript - 如何将 ng-class 与其中包含 `-` 的 css 类一起使用?

css - 无框网格定位/ float 样式

html - Google map 和 Contact Form 7 对齐

javascript - 如何让 Flexslider2 停止跳跃?

javascript - 如何防止单选按钮中的多重选择?

CSS - font-family - 使用 webfont-service 回退?