如何使用添加选项并排创建依赖下拉列表

时间:2015-11-06 08:04:08

标签: javascript jquery html css3

我想并排添加2个下拉列表。就像HTML5,CSS3,JS,PHP等技能的第一次下拉,以及诸如初学者,高级,专家等级别的第二次下拉。我还想要一个添加按钮,这样我就可以继续添加更多技能。下面的代码帮助我添加2个按钮,但垂直而不是旁边。但我想知道如何添加一个按钮。

<label for="skills">Skills</label>
<div id="example1" class="bs-docs-example">
<select class="step1">
<option value="">Please select an option</option>
<option>Option1</option>
<option>Option2</option>
<option>Option3</option>
<option>Option4</option>
</select>
</div>

<label for="skills">Level</label>
<div id="example2" class="bs-docs-example1">
<select class="step2">
<option value="">option</option>
<option>Option5</option>
<option>Option6</option>
<option>Option7</option>
<option>Option8</option>
</select>
</div>

3 个答案:

答案 0 :(得分:0)

添加按钮会在技能选择框中添加“abc”选项。

$( "#add" ).click(function() {
  $('#one').append( new Option("abc","abc") );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="width: 100%; overflow: hidden;">
    <div style="width: 70%; float: left;" id="example1"class="bsdocs-example"> 
        <label for="skills">Skills</label>
        <select id="one" class="step1">
            <option value="">Please select an option</option>
            <option>Option1</option>
            <option>Option2</option>
            <option>Option3</option>
            <option>Option4</option>
         </select>
    </div>
    <div style="margin-left: 30%" id="example2"> 
        <label for="skills">Level</label>
        <select class="step2">
            <option value="">option</option>
            <option>Option5</option>
            <option>Option6</option>
            <option>Option7</option>
            <option>Option8</option>
            </select>
    </div>
    <div style="margin-top:100px" id="example2">
        <button id="add"> add </button>
    </div>
</div>

答案 1 :(得分:0)

试试这个...我希望这是你的预期结果:)

<table>
    <tr>
        <td><label for="skills">Skills</label></td>
        <td colspan="2">
        <select id="one" class="step1">
            <option value="">Please select an option</option>
            <option>Option1</option>
            <option>Option2</option>
            <option>Option3</option>
            <option>Option4</option>
        </select></td>
        <td><label for="skills">Level</label></td>
        <td colspan="2">
        <select id="two" class="step2">
            <option value="">option</option>
            <option>Option5</option>
            <option>Option6</option>
            <option>Option7</option>
            <option>Option8</option>
        </select></td>
        <td><button onclick="addSkill()"> add skill </button></td>
    </tr>
</table>
<label id="currentSkills"></label>

的javascript

function addSkill(){
    var newSkill=$('#one').val();
    var newLevel=$('#two').val();
    console.log(newSkill);
    $('#currentSkills').append(newSkill+'-'+newLevel+"<br>");
}

答案 2 :(得分:0)

试试这个......

$( "#add" ).click(function() {
   var row = $("#wrapper-1").clone();
    //row.remove("#add");
  $('#wrapper').append(row);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<div id="wrapper" width="100%">
    <div id="wrapper-1" style="float: left; width: 85%;">
    <div id="first" style="float: left; width: 65%;"> 
        <label for="skills">Skills</label>
        <select id="one" class="step1">
            <option value="">Please select an option</option>
            <option>Option1</option>
            <option>Option2</option>
            <option>Option3</option>
            <option>Option4</option>
         </select>
    </div>
    <div class="general" style="float: left; width: 35%;"> 
        <label for="skills">Level</label>
        <select class="step2">
            <option value="">option</option>
            <option>Option5</option>
            <option>Option6</option>
            <option>Option7</option>
            <option>Option8</option>
            </select>
    </div>
    </div>
      <div class="add-general" style="float: left; width: 15%;">
        <button id="add"> add </button>
    </div>  
 </div>