doxrxwn2252 2016-06-24 20:30
浏览 36

使用JS添加新的下拉菜单

I am looking to use JS to add a new dropdown menu with a button click, where the dropdown menu's elements create a text box when clicked on.

HTML:

<input id="btnAdd" type="button" value="Add" />
<br />
<br />
<div id="TextBoxContainer">
</div>
<br />

JS:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
    var click_number = 0;
    $("#btnAdd").bind("click", function () {
        var div = $("<div />");
        var input = $("<div id='search'><label>Input &nbsp;<textarea rows='1' name='input_"+ click_number +"'><?php echo $_POST['input_". click_number ."']; ?></textarea></label></div>");
        input.appendTo('#TextBoxContainer');
        div.html(RemoveDynamicTextBox());
        $("#TextBoxContainer").append(div);
        console.log(click_number);
        click_number++;
    });
    $("body").on("click", ".remove", function () {
        for ($i=0; $i<2; $i++){
            $('#search').remove();
        }
        click_number--;
        console.log(click_number);
    });

});

function RemoveDynamicTextBox() {

    return "<input id='search' type='button' value='Remove' class='remove' />";

}

</script>

Currently, the above code adds a textbox and remove button with each button click. I then use the textbox as a post variable input while containing this all within a form with a submit button so I can use the input in a mysqli query. I increment the name of the post variable with the click number to distinguish the inputs later in the query.

I am new to JS (so if I haven't provided enough info, let me know and i'll edit), but I would like to have a lot of different options for input text boxes in a dropdown menu that is added with the button click. Then when the user clicks on an element from the dropdown menu, it is added as a text box input. So, each time the user clicks the add button, a new dropdown menu would appear. Thus, in theory I could have 2 of the same inputs with different increment in their name that could be differentiated later. Also, I would have the remove button remove both the added text boxes and the dropdown menu associated with the last "add" button click.

  • 写回答

1条回答 默认 最新

  • duanqiechui2378 2016-06-24 20:56
    关注

    Use bootstrap, it will make it very easy to do. Copy this into a text document, save it as an .html and look at the result.

    <!DOCTYPE html>
    <html>
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    </head>
    <body>
    
    <div class="container">
      <h2>Dropdowns</h2>
      <p>The .dropdown class is used to indicate a dropdown menu.</p>
      <p>Use the .dropdown-menu class to actually build the dropdown menu.</p>
      <p>To open the dropdown menu, use a button or a link with a class of .dropdown-toggle and data-toggle="dropdown".</p>
      <div class="dropdown">
        <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example
        <span class="caret"></span></button>
        <ul class="dropdown-menu">
          <li><label>Textbox:</label><input type="text"></input></li>
    
        </ul>
      </div>
    </div>
    
    </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用