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

标签 jquery html css

我是 jquery 的新手,我尝试了一些代码来禁用按钮。但是,它对我不起作用。
只有从以上三个框中选择了任意三个值(每个框一个值),我才想启用“添加类别”按钮,并且我想启用“删除类别” "& "Save Categories"按钮仅当第四个框中有任何值时。

var one = $('.select-manage-category').val();
var two = $('.select-manage-category1').val();
var three = $('.select-manage-category2').val();
$('#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);
    }
});
$('#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 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>

最佳答案

您需要将 change 事件添加到所有三个 select-manage-category 列表和您的 selected-lst-values 列表,如代码段所示!

希望这能回答您的问题!

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>

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

相关文章:

javascript - jQuery.noty 未定义

html - 如何将文本放在预先存在的边框内

javascript - 使用 JavaScript Cookie 的简单购物车

javascript - js getContext 停止执行进一步的代码

css - 在 CSS 中的列之间填充

css - 在 css 上内联显示内容?

javascript - 如何在 HTML JS 中获取外部文本

c# - 获取 ScriptHandlerFactory 处理程序

javascript - 使用 JQuery Range Slider 实时更新 CSS 值

javascript - 从左到右动画宽度(带文字)