删除<option>后更新计数器

时间:2018-03-05 07:15:03

标签: javascript jquery

我有一个脚本,当项目添加到下拉列表时,会增加计数器。这工作正常。但是,如果项目从列表中删除,我需要一种方法来减少计数器。

我正在努力找到一种方法来使用我的代码来做到这一点,如果有人可以帮我编码,我会很感激。我使用jQuery选择的插件从按钮单击填充列表。我已经发布了js,但如果你需要HTML,我很乐意发布。只需发布js代码即可将帖子保持在最低限度。非常感谢。

JS

$(function() {
  $(document).on('click', '#add', function() {
    var boxvalue = $("#box_input").val();

    if (boxvalue ==''){
      $("#niinputmessage").fadeIn(3000).html('No blank entries').fadeOut(5000).css({ 'color': 'red', 'margin-left': '5px', 'margin-top': '5px' });
      return false;
    }

    var count = $("#box_ni :selected").length;
    $("#counter").html("Total selected boxes for intake: " + 
    '<span style="font-size: 14px; color: white;">' + 
    '( ' + count + ' )' + 
    '</span>').css('color:, black');

    if (count > 2) {
      $("#counter").html("No more than 3 items per intake");
      return false;
    } else {
      count++;
      $("#counter").html("Total selected boxes for intake: " + 
      '<span style="font-size: 14px; color: white;">' + '( ' + count + ' )' + 
      '</span>').css('color:, black');
    }

    $("#box_ni").attr("data-placeholder", "Select boxes for intake... ");
    $("#box_ni").append("<option selected>" + boxvalue + "</option>");
    $("#box_ni").trigger("chosen:updated");
    $("#box_input").val('');
    });
  });

HTML

<div class="form-group">
    <label for="box_input" class="labelStyle">Enter Your Box(es)</label><br />
    <input type="text" id="box_input" name="box_input" disabled />
    <input type="button" id="add" Value="Add" disabled />
    <div id="niinputmessage"></div>
        <div class="servicesHelp"><lead id="serviceHelp" class="form-text text-muted">Please select your boxes from the list. You can select a max of 3 boxes per submission. You can select multiple boxes by holding the left ctrl on your keyboard and making your selection</lead>
        </div>
        <div class="noBrtvBoxes" style="color:white;"></div>
  </div>
  <div id="counter" style="margin-left: 7px; font-size: 14px; color: #c5ba7a;">Total selected boxes for intake: 0</div><br />
  <div class="message">
    <div class="sucMsg">

    </div>
  </div>

1 个答案:

答案 0 :(得分:1)

您不需要递增计数器,您可以通过以下方式获得选项数量:

selectElement.options.length

要获取当前选定的选项,您可以通过按钮将其删除。

selectElement.options[selectElement.selectedIndex]

在下面的代码中,我只是在您通过“更改”选择它时将其删除。事件,我无法使用&#39;点击&#39;事件,因为当你打开列表时它将删除当前,我必须保留无用的选项,以使用户可以更改最后一个。

&#13;
&#13;
const addBtn = document.querySelector('.add')
  const inputEl = document.querySelector('.input-box')
  const list = document.querySelector('.list')
  const counter = document.querySelector('.counter')
  const mode = document.querySelector('.mode')
  const max = 3
  let num = 0
  addBtn.addEventListener('click', e => {
    if (addBtn.disabled) {
      return false
    }
    if (inputEl.value.trim().length === 0) {
      return
    }
    addItem()
    
    updateCounter()
    resetInput()
    num+=1
    if (num >= max) {
      inputEl.disabled = true
      addBtn.disabled = true
    }
    e.preventDefault()
  })

  function updateCounter () {
    counter.textContent = list.options.length - 1
  }

  function addItem () {
    const option = document.createElement('option')
    option.value = option.text = inputEl.value.trim()
    list.appendChild(option)
  }

  function resetInput () {
    inputEl.value = ''
  }
  const warn = document.querySelector('.warn')
  mode.addEventListener('click', e => {
    if (mode.checked) {
      warn.classList.add('active')
      list.classList.add('removing')
      list.selectedIndex = 0
      list.focus()
      
    } else {
      warn.classList.remove('active')
      list.classList.remove('removing')
    }
  })

  list.addEventListener('change', e => {
    if (!mode.checked) {
      return false
    }

    if (list.selectedIndex == 0) {
      return false
    }
    const selected = list.options[list.selectedIndex]
    selected.parentNode.removeChild(selected)
    updateCounter()
    e.preventDefault()
  })
&#13;
select {
    outline: none;
  }
  .view {
    margin: 0 auto;
    width: 600px;
  }
  label {
    width: 100px;
    margin-right: 5px;
  }
  .form-control {
    margin: 15px 0 15px 0;
  }

  .removing {
    border: 1px solid red;
  }

  .warn {
    visibility: hidden;
    background: red;
    color: #FFF;
  }
  .warn.active {
    visibility: visible;
  }
&#13;
<div class="view">
  <form>
      <div class="form-control">
        <label>new option</label>
        <input class="input-box" type="text" placeholder="new option here">
        <button class="add">Add to options</button>
      </div>
      <div class="form-control">
        <label>All options:</label>
        <select class="list">
          <option selected="selected">All options</option>
        </select>
        <input type="checkbox" class="mode">
        <label>Removing mode</label>
        <div class="warn">click dropdown option will remove it</div>
      </div>
  </form>
  <div class="output">
    counts: <span class="counter">0</span>
  </div>
</div>
&#13;
&#13;
&#13;