在bootstrap中创建交互式文本框

时间:2015-05-03 08:43:55

标签: javascript jquery css ajax twitter-bootstrap

我想在我的网站上为ajax搜索结果制作一个交互式文本框。我想调用ajax并在文本框中显示结果。

HTML code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>INIK </title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <?php 
      session_start();
     ?>
    <nav class="navbar navbar-inverse">
      <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Brand</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">HOME <span class="sr-only">(current)</span></a></li>

            <?php if ( isset($_SESSION['username'])) 
              {
            ?>
              <li><a href="logout.php">Logout </a></li>
            <?php 
              }
            ?>

          </ul>
          <form class="navbar-form navbar-right"  role="search">
            <div class="form-group">
              <input type="text" id="search_user" class="form-control" placeholder="Search">
            </div>
            <button type="submit" class="btn btn-default">Submit</button>

          </form>


        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>
    <div class="navbar-form navbar-right" id="search_list">

    </div>

现在,id search_List的css为:

#search_list 
{
    /*background: #000000;*/
    /*display: block;*/
    /*overflow: auto;*/
    position: absolute;

    right: 22%;
    z-index: 99;
}

在其中添加元素的ajax是:

$('#search_user').on('keyup',
    function (e) {
        e.preventDefault();

         var search_text = $('#search_user').val();
        // var text = $('#post-text').val();

        // alert("hello from keyup" + search_text);

         // alert(username);
        $.ajax(
        {
            url:'search.php',
            type:'POST',
            dataType:"json",
            data: { search_text : search_text },
            success:function(data)
            {
                $('#search_list').text("");
                $.each(data, function(i, item) 
                {
                    $('#search_list').addClass("alert alert-success");
                    $('#search_list').append(item.user_name + "<br>" );
                    // var html = '<span class="fa fa-minus delete_modal_button">delete</span> <br>';

                    // $('#list').append(html);
                  // alert();
                });


            },
            error : function() { console.log(arguments); }
        }
            );
    });

z-index看起来不错,但文本框的宽度导致问题。它在改变设备宽度方面效果不佳。请帮我解决这个问题。

0 个答案:

没有答案