简体   繁体   English

使用jQuery从选择框中删除选择的值,但需要在选择框中显示当前选择的值吗?

[英]remove selected value from select box using jquery but It's need to display current selected value in select box?

I have a select box and add more button . 我有一个select框并添加更多button when I click add more button it's creating another select using clone.In first select box I select one option value from select box means that value should be removed from next created select box.At the same time which selected value in select box that current value shown on current select box. 当我单击添加更多button它正在使用克隆创建另一个select在第一个选择框中,我从select框中select一个option值,意味着应从下一个创建的select框中删除该值。同时,在选择框中选择的值即为当前value显示在当前select框上。 Select box value is being loaded dynamically. 选择框值正在动态加载。

Eg: 例如:

<select name="section" id="section_1" class="sectionType">
    <option value=" ">------</option>
    <option value="05">test1</option>
    <option value="06">test2</option>
    <option value="07">test3</option>
    <option value="08">test4</option>
    <option value="10">test5</option>
    <option value="11">test6</option>
    <option value="12">test7</option>
    <option value="13">test8</option>
    <option value="14">test9</option>
</select>

Is it what you're looking for ? 您在找什么吗?

I would recommend you to play and manipulate with index() , that won't bother your dynamic values. 我建议您使用index()进行播放和操作,这样不会影响您的动态值。

//Take a clone of last 
var cloneElement = $('.sectionType:last').clone();
//Get index of option selected from last 
var indexToRemove = $('.sectionType:last').find('option:selected').index();
//Remove previously selected index
cloneElement.find('option').eq(indexToRemove).remove();
//Change the id of an element
cloneElement.attr("id", "section_"+parseInt($('.sectionType').length+1));
//If element has options
if(cloneElement.find('option').length)
{
  //Finally append it
  $('body').append("<br/><br/>").append(cloneElement);
}

 $('button').click(function(){ //Take a clone of last var cloneElement = $('.sectionType:last').clone(); //Get index of option selected from last var indexToRemove = $('.sectionType:last').find('option:selected').index(); //Remove previously selected index cloneElement.find('option').eq(indexToRemove).remove(); //Change the id of an element cloneElement.attr("id", "section_"+parseInt($('.sectionType').length+1)); //If element has options if(cloneElement.find('option').length) { //Finally append it $('body').append("<br/><br/>").append(cloneElement); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="section" id="section_1" class="sectionType"> <option value="">------</option> <option value="05">test1</option> <option value="06">test2</option> <option value="07">test3</option> <option value="08">test4</option> <option value="10">test5</option> <option value="11">test6</option> <option value="12">test7</option> <option value="13">test8</option> <option value="14">test9</option> </select> <button>Clone</button> 

You can try like this. 您可以这样尝试。

$("#yourId").val(" ")//if your value has white spec else use like below line
$("#YourId").val("")//What ever you want to be selected, place your value in .val()

I hope this will help you, if you need anything please ask! 希望对您有所帮助,如果您需要任何帮助,请询问!

 $("button").on("click", function() { $("#section_1") .clone() .attr("id", "section_2") .on("change", function() { var sec2Val = $(this).val(); var delOption = $("#section_1 > option[value=" + sec2Val + "]").detach(); optionHolder.push(delOption); }) .insertAfter($("#section_1")); $(this).attr("disabled", "disabled"); }); var optionHolder = []; $("#section_1").on("change", function() { var sec1Val = $(this).val(); if ($("#section_2")) { var delOption = $("#section_2 > option[value=" + sec1Val + "]").detach(); optionHolder.push(delOption); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="section" id="section_1" class="sectionType"> <option value=" ">------</option> <option value="05">test1</option> <option value="06">test2</option> <option value="07">test3</option> <option value="08">test4</option> <option value="10">test5</option> <option value="11">test6</option> <option value="12">test7</option> <option value="13">test8</option> <option value="14">test9</option> </select> <button>Add more</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM