使用jQuery添加/删除select

时间:2016-01-19 08:58:06

标签: javascript jquery

我构建了这个代码,为了添加和删除选择感谢按钮,但是当我点击它时,没有任何反应。

更新:

我的HTML:

<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
<div>
<select class="form-control" name="competence" id="competence">
  <?php foreach ($resDisplayCompetence as $ligne) { ?>
  <option value="<?php echo $ligne['id'] ?>"><?php echo $ligne['name'] ?></option>
  <?php } ?>
</select>

我的jQuery:

$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID

var x = 1; //initlal text box count
$(add_button).click(function(e) { //on add input button click
e.preventDefault();
if (x < max_fields) { //max input box allowed
  x++; //text box increment
  $(wrapper).append(
    '<select class="form-control" name="competence" id="competence"><option value="">test</option></select>'); //add input box
}
});

$(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
e.preventDefault();
$(this).parent('div').remove();
x--;
})
});

我想知道是否可以使用这个jQuery代码使用PHP添加动态选择。

2 个答案:

答案 0 :(得分:0)

https://www.safaribooksonline.com/library/view/jquery-cookbook/9780596806941/ch10s07.html找到这篇文章,这可能会帮助您解决问题。

<强> HTML:

<label for="form-control">Form Control</label>
<select class="form-control" name="competence" id="competence">
    <option>Option 1</option>
</select>
<br>
<br>

<button id="remove">Remove</button>
<br>
<br>

<label for="newFieldName">New Select</label>
<input id="newFieldName" type="text" />

<label for="newFieldValue">Value</label>
<input id="newFieldValue" type="text" />

<button id="add">Add</button>

<强> jQuery的:

// find the "Add New" button
$('#add').click(function(event){
    event.preventDefault();
    //could be static values
    var optionName = $('#newFieldName').val();
    var optionValue = $('#newFieldValue').val();
    $('<option/>').attr('value',optionValue).text(optionName).appendTo('#competence');
});


// find the "Remove Selected" button
$('#remove').click(function(event){
    event.preventDefault();
    var $select = $('#competence');
    $('option:selected',$select).remove();
});

jsFiddle - https://jsfiddle.net/0d83L0q8/

答案 1 :(得分:0)

我认为你在寻找的是

$(document).ready(function() {
  var max_fields = 10; //maximum input boxes allowed
  var wrapper = $(".input_fields_wrap"); //Fields wrapper
  var add_button = $(".add_field_button"); //Add button ID
  var $select = $('#competence');

  var x = 1; //initlal text box count
  add_button.click(function(e) { //on add input button click
    e.preventDefault();
    if (x < max_fields) { //max input box allowed
      x++; //text box increment
      $select.clone().removeAttr('id').appendTo(wrapper.find('div'));
    }
  });

  $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
    e.preventDefault();
    if (x > 1) {
      x--;
      wrapper.find('select:not(#competence)').last().remove();
    }
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input_fields_wrap">
  <button class="add_field_button">Add More Fields</button>
  <button class="remove_field">Remove More Fields</button>
  <div>
    <select class="form-control" name="competence" id="competence">
      <option value="">test</option>
      <option>1</option>
      <option>2</option>
      <option>3</option>
    </select>
  </div>
</div>