页面加载更改输入值和触发器

时间:2017-05-10 05:51:33

标签: javascript jquery html list.js

我正在使用list.js.当页面加载时,我想将搜索栏值更改为“newvalue”,然后触发搜索。应该发生的是没有列表项,因为没有匹配搜索项。不幸的是,虽然我已经能够让搜索栏输入值改变,但我似乎无法触发实际的搜索本身。我已经尝试在值更改之前关注input元素,我已经尝试设置超时功能,并且我尝试使用.change而不是.val。我不确定是什么问题,在这一点上我不确定还能做什么,所以任何帮助都将不胜感激。

//On page load change search value & simulate enter key press
$("document").ready(function() {
        $('input[name=searchBox]').val('newvalue').trigger(jQuery.Event('keypress', { keycode: 13 })); 
});





//List.js Stuff
var options = {
  valueNames: ['material', 'type', 'thickness', 'height', 'category', 'date']
};

var featureList = new List('piece-search', options);

$('#filter-material').change(function() {
  var selection = this.value;
});
.item {
  margin: 0.5em;
  padding: 0.5em;
  width: 150px;
  height: 150px;
  float: left;
  background: #229B55;
  color: #F4F4F4;
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.15);
}

.item p {
  margin: 0;
  padding: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/1.1.1/list.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
  <div class="page">
    <div id="main">
      <div class="c1">
        <div id="piece-search">
          <ul class="sort-by">
            <li>
              <input class="search" placeholder="Search pieces" name="searchBox">
            </li>
          </ul>
          <ul class="list">

            <li class="item">
              <p class="sorting-info hide-this">
                <p class="material">plastic</p>
                <p class="type">pipe</p>
                <p class="thickness">3mm</p>
                <p class="height">15inch+</p>
                <p class="category">artsy</p>
                <p class="date">20170101</p>
            </li>

            <li class="item">
              <p class="sorting-info hide-this">
                <p class="material">glass</p>
                <p class="type">pipe</p>
                <p class="thickness">5mm</p>
                <p class="height">14inch-</p>
                <p class="category">scientific</p>
                <p class="date">20170107</p>
            </li>
            
            <li class="item">
              <p class="sorting-info hide-this">
                <p class="material">glass</p>
                <p class="type">pipe</p>
                <p class="thickness">7mm</p>
                <p class="height">16inch-</p>
                <p class="category">scientific</p>
                <p class="date">20170209</p>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>

1 个答案:

答案 0 :(得分:0)

你应该使用list api,使用jquery trigger可能无法正常调用list函数。见http://listjs.com/api/#search

 $("document").ready(function() {
            $('input[name=searchBox]').val('newvalue');
            featureList.search('newvalue');
    });