当我选择特定项目时,我有一个项目列表它应该出现所有应该出现在Javascript或j查询给出一个想法

时间:2017-07-20 04:47:40

标签: javascript jquery ajax

  1. 电池
  2. 更换汽车电池
  3. 服务电池/电缆
  4. 蛇形/传动带更换
  5. 服务电池/电缆
  6. xxx
  7. 当我点击应该出现的电池时,其他物品应该消失。

     function serviceid(id){
        var searchid=id;
        alert(searchid); 
         i f (searchid!=="")
         {
              if(document.getElementById("myText").value!=="")
              {
                  document.getElementById("myText").value+=","+searchid;
                  var x = document.getElementById('myText');
                  if (x.style.display === 'none') 
                  {
                    x.style.display = 'block';
                      }
                          else 
                      {
                         x.style.display = 'none';
                      }
            }
            else{
                 document.getElementById("myText").value+=searchid;
            }
        }   //document.getElementById("myText").value+=','+searchid;  
        else{
            document.getElementById("myText").value+=searchid;
            alert(document.getElementById("myText").value);
        }
    }
    

4 个答案:

答案 0 :(得分:0)

尝试这个

$(document).ready(function() {
  $('ol li').click(function() {
    $("ol li").not(this).remove();
  });
});
ol li {
  cursor: pointer;
  padding: 3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ol>
  <li>Battery</li>
  <li>Car Battery Replacement</li>
  <li>Service Battery/cables</li>
  <li>Serpentine/Drive Belt Replacement</li>
  <li>Service Battery/cables</li>
  <li>xxx</li>
</ol>

我觉得它有用

答案 1 :(得分:0)

试试这个:

<html>
<head>
</head>
<body>
    <ol>
        <li>a</li>
        <li>b</li>
        <li>c</li>
    </ol>
    <script src="jquery.js"></script>
    <script>
        $( "li" ).click(function() {
            $("li").hide();
            $( this ).show();
        });
    </script>
</body>

答案 2 :(得分:0)

这是一个javascript解决方案,当你点击一个li元素时,它会显示它并隐藏其余的li元素。

var li = document.getElementsByTagName('li');
Array.from(li).forEach( function(element) {
  element.addEventListener("click", function(event){
      Array.from(li).forEach(function(element){
        if(element === event.target){
          element.style.display = 'list-item';
        } else {
          element.style.display = 'none';
        }
      });
  });
});
<ul>
  <li>Battery</li>
  <li>Car Battery Replacement</li>
  <li>Service Battery/cables</li>
  <li>Serpentine/Drive Belt Replacement</li>
  <li>Service Battery/cables</li>
  <li>xxx</li>
</ul>

答案 3 :(得分:0)

使用index()eq()的最简单的单行解决方案:

&#13;
&#13;
$('ol li').on('click', function() {
  $('ol li').hide().eq($(this).index()).show();
});
&#13;
ol li {
  cursor: pointer;
  padding: 3px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ol>
  <li>Battery</li>
  <li>Car Battery Replacement</li>
  <li>Service Battery/cables</li>
  <li>Serpentine/Drive Belt Replacement</li>
  <li>Service Battery/cables</li>
  <li>xxx</li>
</ol>
&#13;
&#13;
&#13;