javascript - 选择 <li> 并将值传递到文本字段

标签 javascript html

我有一个网站,其中有一个搜索栏,我正在其中实现。搜索栏正确显示数据库的输出(为了更容易理解,我“硬编码”了一些输出)。但是,我似乎无法单击显示的搜索项,并且 onclick 事件不起作用,它只是从焦点中删除列表并且不显示它。

function set_item(item) {
  alert(item);
  $('#country_id').val(item);

  $('#country_list_id').hide();
}
body {
  background: #f7f7f7;
  color: #404040;
  font-family: 'HelveticaNeue', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-size: 13px;
  font-weight: normal;
  line-height: 20px;
}

a {
  color: #1e7ad3;
  text-decoration: none;
}

a:hover {
  text-decoration: underline
}

.container,
.main {
  width: 640px;
  margin-left: auto;
  margin-right: auto;
  height: 300px;
}

.main {
  margin-top: 50px
}

input {
  font-family: 'HelveticaNeue', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-size: 13px;
  color: #555860;
}

.search {
  position: relative;
  margin: 0 auto;
  width: 300px;
}

.search input {
  height: 26px;
  width: 100%;
  padding: 0 12px 0 25px;
  background: white url("http://cssdeck.com/uploads/media/items/5/5JuDgOa.png") 8px 6px no-repeat;
  border-width: 1px;
  border-style: solid;
  border-color: #a8acbc #babdcc #c0c3d2;
  border-radius: 13px;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  -ms-box-sizing: border-box;
  -o-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-box-shadow: inset 0 1px #e5e7ed, 0 1px 0 #fcfcfc;
  -moz-box-shadow: inset 0 1px #e5e7ed, 0 1px 0 #fcfcfc;
  -ms-box-shadow: inset 0 1px #e5e7ed, 0 1px 0 #fcfcfc;
  -o-box-shadow: inset 0 1px #e5e7ed, 0 1px 0 #fcfcfc;
  box-shadow: inset 0 1px #e5e7ed, 0 1px 0 #fcfcfc;
}

.search input:focus {
  outline: none;
  border-color: #66b1ee;
  -webkit-box-shadow: 0 0 2px rgba(85, 168, 236, 0.9);
  -moz-box-shadow: 0 0 2px rgba(85, 168, 236, 0.9);
  -ms-box-shadow: 0 0 2px rgba(85, 168, 236, 0.9);
  -o-box-shadow: 0 0 2px rgba(85, 168, 236, 0.9);
  box-shadow: 0 0 2px rgba(85, 168, 236, 0.9);
}

.search input:focus + .results {
  display: block
}

.search .results {
  display: none;
  position: absolute;
  top: 35px;
  left: 0;
  right: 0;
  z-index: 10;
  padding: 0;
  margin: 0;
  border-width: 1px;
  border-style: solid;
  border-color: #cbcfe2 #c8cee7 #c4c7d7;
  border-radius: 3px;
  background-color: #fdfdfd;
  background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fdfdfd), color-stop(100%, #eceef4));
  background-image: -webkit-linear-gradient(top, #fdfdfd, #eceef4);
  background-image: -moz-linear-gradient(top, #fdfdfd, #eceef4);
  background-image: -ms-linear-gradient(top, #fdfdfd, #eceef4);
  background-image: -o-linear-gradient(top, #fdfdfd, #eceef4);
  background-image: linear-gradient(top, #fdfdfd, #eceef4);
  -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  -ms-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  -o-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.search .results li {
  display: block
}

.search .results li:first-child {
  margin-top: -1px
}

.search .results li:first-child:before,
.search .results li:first-child:after {
  display: block;
  content: '';
  width: 0;
  height: 0;
  position: absolute;
  left: 50%;
  margin-left: -5px;
  border: 5px outset transparent;
}

.search .results li:first-child:before {
  border-bottom: 5px solid #c4c7d7;
  top: -11px;
}

.search .results li:first-child:after {
  border-bottom: 5px solid #fdfdfd;
  top: -10px;
}

.search .results li:first-child:hover:before,
.search .results li:first-child:hover:after {
  display: none
}

.search .results li:last-child {
  margin-bottom: -1px
}

.search .results a {
  display: block;
  position: relative;
  margin: 0 -1px;
  padding: 6px 40px 6px 10px;
  color: #808394;
  font-weight: 500;
  text-shadow: 0 1px #fff;
  border: 1px solid transparent;
  border-radius: 3px;
}

.search .results a span {
  font-weight: 200
}

.search .results a:before {
  content: '';
  width: 18px;
  height: 18px;
  position: absolute;
  top: 50%;
  right: 10px;
  margin-top: -9px;
  background: url("http://cssdeck.com/uploads/media/items/7/7BNkBjd.png") 0 0 no-repeat;
}

.search .results a:hover {
  text-decoration: none;
  color: #fff;
  text-shadow: 0 -1px rgba(0, 0, 0, 0.3);
  border-color: #2380dd #2179d5 #1a60aa;
  background-color: #338cdf;
  background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #59aaf4), color-stop(100%, #338cdf));
  background-image: -webkit-linear-gradient(top, #59aaf4, #338cdf);
  background-image: -moz-linear-gradient(top, #59aaf4, #338cdf);
  background-image: -ms-linear-gradient(top, #59aaf4, #338cdf);
  background-image: -o-linear-gradient(top, #59aaf4, #338cdf);
  background-image: linear-gradient(top, #59aaf4, #338cdf);
  -webkit-box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
  -moz-box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
  -ms-box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
  -o-box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
  box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
}

:-moz-placeholder {
  color: #a7aabc;
  font-weight: 200;
}

::-webkit-input-placeholder {
  color: #a7aabc;
  font-weight: 200;
}

.lt-ie9 .search input {
  line-height: 26px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<section class="main">
  <form class="search">
    <input type="text" name="q" id="country_id" onkeyup="autocomplet()" />
    <ul class="results">
      <div id="country_list_id">
        <li onclick="set_item('349')">
          <p hidden="" id="hiddennr">349</p><a>SKY News<br><span>Hispasat 1D/1E (30.0W)</span></a></li>
        <li onclick="set_item('350')">
          <p hidden="" id="hiddennr">350</p><a>SKY News<br><span>Hispasat 1D/1E (30.0W)</span></a></li>
        <li onclick="set_item('934')">
          <p hidden="" id="hiddennr">934</p><a>ALASKANDRIA<br><span>Nilesat102/201 - Eutelsat 7WA (7.0W)</span></a></li>
        <li onclick="set_item('1542')">
          <p hidden="" id="hiddennr">1542</p><a>Sky News Arabia<br><span>Nilesat102/201 - Eutelsat 7WA (7.0W)</span></a></li>
        <li onclick="set_item('1543')">
          <p hidden="" id="hiddennr">1543</p><a>Sky News Arabia HD<br><span>Nilesat102/201 - Eutelsat 7WA (7.0W)</span></a></li>
        <li onclick="set_item('1544')">
          <p hidden="" id="hiddennr">1544</p><a>Sky News HD<br><span>Nilesat102/201 - Eutelsat 7WA (7.0W)</span></a></li>
        <li onclick="set_item('1937')">
          <p hidden="" id="hiddennr">1937</p><a>Sky TG24 HD<br><span>Eutelsat 5WA (5.0W)</span></a></li>
        <li onclick="set_item('1978')">
          <p hidden="" id="hiddennr">1978</p><a>2000 - ESKA SES<br><span>Amos 2/3 (4.0W)</span></a></li>
        <li onclick="set_item('1979')">
          <p hidden="" id="hiddennr">1979</p><a>2010 - ESKA Belchatow<br><span>Amos 2/3 (4.0W)</span></a></li>
        <li onclick="set_item('1980')">
          <p hidden="" id="hiddennr">1980</p><a>2020 - ESKA Bialystok<br><span>Amos 2/3 (4.0W)</span></a></li>
      </div>
    </ul>
  </form>
</section>

function autocomplet() {
var min_length = 2; 
var keyword = $('#country_id').val();
if (keyword.length >= min_length) {
    $.ajax({
        url: 'ajax_refresh.php',
        type: 'POST',
        data: {keyword:keyword},
        success:function(data){
            $('#country_list_id').show();
            $('#country_list_id').html(data);
        }
    });
} else {
    $('#country_list_id').hide();
}
}


$pdo = connect();
$keyword = '%'.$_POST['keyword'].'%';
$sql = "SELECT * FROM mychannels WHERE Service_Name LIKE (:keyword) ORDER BY      idchannel ASC LIMIT 0, 10";
$query = $pdo->prepare($sql);
$query->bindParam(':keyword', $keyword, PDO::PARAM_STR);
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) {

$country_name = str_replace($_POST['keyword'], '<b>'.$_POST['keyword'].'</b>', $rs['Service_Name']);


echo  '<li onclick="set_item(\''.str_replace("'", "\'", $rs['idchannel']).'\')"><p hidden id="hiddennr">'.$rs['idchannel'].'</p><a>'.$rs['Service_Name'].'<br /><span>'.$rs['Satellite_name'].'</span></a></li>';
}

This is the fiddle

我想要实现的目标是:我想单击一个

  • 项目,并且我想运行函数 set_item() ,该函数应该在文本中填充数据字段

    Blockquote

  • 最佳答案

    由于(ab)使用CSS技巧来显示/隐藏内容,所以必须写答案。由于某种原因:

    .search input:focus + .results {
      display: block;
    }
    
    CSS 中的

    是问题的原因(?)。单击 LI 元素根本没有注册,因此当单击 .results 列表时,不会调用函数 set_item()。删除指定行后,一切正常。

    演示:https://jsfiddle.net/m70jgcrj/42/

    附注使用 jQuery 显示/隐藏列表也好一点:

    $('#country_id').click(function() {
    
    $('.results').slideDown();
    });
    
    $('.results').click(function() {
    
    $(this).slideUp();
    });
    

    这是 js/CSS 冲突的有趣案例(?),也是应该避免(恕我直言)CSS“事件”(“checkbox hack”、:focus 和:active 技巧)的原因。 CSS用于样式,js用于交互。 :)

    关于javascript - 选择 <li> 并将值传递到文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40856315/

    相关文章:

    html - 如何在不使用背景放入 html 的情况下将关闭按钮放在 div 上?

    javascript - 使用 Nodejs 渲染 html-css-javascript-images

    javascript - 尝试使用 JQuery 构建具有可扩展行(嵌入式表)的表

    javascript - 动态添加输入字段,但字段由外部 PHP 函数生成

    javascript - 何时不使用 promise

    javascript - 使用 $compileProvider 避免不安全链接

    javascript - 如何指定运行嵌套 Web 组件构造函数的顺序?

    javascript - 不要使用后退按钮让弹出窗口出现

    html - 加载到 TWebBrowser 中的 html 文档的总高度

    html - 何时在 Django 中使用 as_hidden,为什么不只显示 : none; in CSS?