jquery - 如何在打开或关闭按钮中使用 Jquery 来限制自动列表

标签 jquery html css

如果我们在名称文本框中按任意数字,将显示一个列表。但是这个自动列表应该只在搜索按钮打开时显示,否则我们必须手动输入。我如何在 JQuery 中执行此操作?

<!DOCTYPE>
<html>
<head>
<style>

.onoffswitch {
    position: relative; width: 63px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    border: 2px solid #999999; border-radius: 20px;
}
.onoffswitch-inner {
    display: block; width: 200%; margin-left: -100%;
    transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    display: block; float: left; width: 50%; height: 30px; padding: 0; line-height: 30px;
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
    box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "ON";
    padding-left: 6px;
    background-color: #85a857; color: #FFFFFF;
}
.onoffswitch-inner:after {
    content: "OFF";
    padding-right: 2px;
    background-color: #EEEEEE; color: #999999;
    text-align: right;
}
.onoffswitch-switch {
    display: block;
    width: 30px;
    margin: 6px;
    background: #FFFFFF;
    position: absolute;
    top: -4px;
    bottom: 0;
    right: 26px;
    border: 2px solid #999999; border-radius: 20px;
    transition: all 0.3s ease-in 0s; 
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: -5px; 
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js"></script>   
</head>
<body>
<table cellpadding="0" cellspacing="0" style="width: 300px;">
                    <tr>
                        <td>Name:</td>
                        <td>                                                        
                            <input list="product_name" name="item_name">
                              <datalist id="product_name">
                                <option value="112-800-00000">
                                <option value="112-700-00000">
                                <option value="700-800-00000">
                                <option value="100-800-00000">
                                <option value="900-800-00000">
                                <option value="600-800-00000">
                                <option value="08000BK07045">
                                <option value="08000BK04045">
                                <option value="08000BK06045">
                              </datalist>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <div class="">
                                <div> Search </div>
                                <div class="onoffswitch dispinline "> 
                                    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
                                    <label class="onoffswitch-label" for="myonoffswitch">
                                        <span class="onoffswitch-inner"></span>
                                        <span class="onoffswitch-switch"></span>
                                    </label>
                                </div>
                            </div>  
                        </td>
                    </tr>
                    <tr><td style="height: 10px;"></td></tr>
                    <tr>
                        <td colspan="2"><input type="submit" value="Add Item to Cart" name="add_item_to_cart"></td>
                    </tr>
                    <tr><td style="height: 10px;"></td></tr>

                </table>
</body>
</html>

最佳答案

我认为您应该在检查开关时忽略数据列表,方法是断开它们之间的唯一链接,即 id,如下所示:

$(document).ready(function(){
  $('#myonoffswitch').change(function(){
    if($(this).is(':checked'))
      $('#mylist').next('datalist').attr('id','product_name');
    else
      $('#mylist').next('datalist').attr('id','product_name1');
  });
});

同时将搜索字段的id作为mylist进行选择

<input list="product_name" name="item_name" id="mylist">

完整的工作片段:

$(document).ready(function(){
  $('#myonoffswitch').change(function(){
    if($(this).is(':checked'))
      $('#mylist').next('datalist').attr('id','product_name');
    else
      $('#mylist').next('datalist').attr('id','product_name1');
  });
});
<!DOCTYPE>
<html>
<head>
<style>

.onoffswitch {
    position: relative; width: 63px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    border: 2px solid #999999; border-radius: 20px;
}
.onoffswitch-inner {
    display: block; width: 200%; margin-left: -100%;
    transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    display: block; float: left; width: 50%; height: 30px; padding: 0; line-height: 30px;
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
    box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "ON";
    padding-left: 6px;
    background-color: #85a857; color: #FFFFFF;
}
.onoffswitch-inner:after {
    content: "OFF";
    padding-right: 2px;
    background-color: #EEEEEE; color: #999999;
    text-align: right;
}
.onoffswitch-switch {
    display: block;
    width: 30px;
    margin: 6px;
    background: #FFFFFF;
    position: absolute;
    top: -4px;
    bottom: 0;
    right: 26px;
    border: 2px solid #999999; border-radius: 20px;
    transition: all 0.3s ease-in 0s; 
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: -5px; 
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js"></script>   
</head>
<body>
<table cellpadding="0" cellspacing="0" style="width: 300px;">
                    <tr>
                        <td>Name:</td>
                        <td>                                                        
                            <input list="product_name" name="item_name" id="mylist">
                              <datalist id="product_name">
                                <option value="112-800-00000">112-800-00000</option>
                                <option value="112-700-00000">112-700-00000</option>
                                <option value="700-800-00000">700-800-00000</option>
                                <option value="100-800-00000">100-800-00000</option>
                                <option value="900-800-00000">900-800-00000</option>
                                <option value="600-800-00000">600-800-00000</option>
                                <option value="08000BK07045">08000BK07045</option>
                                <option value="08000BK04045">08000BK04045</option>
                                <option value="08000BK06045">08000BK06045</option>
                              </datalist>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <div class="">
                                <div> Search </div>
                                <div class="onoffswitch dispinline "> 
                                    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
                                    <label class="onoffswitch-label" for="myonoffswitch">
                                        <span class="onoffswitch-inner"></span>
                                        <span class="onoffswitch-switch"></span>
                                    </label>
                                </div>
                            </div>  
                        </td>
                    </tr>
                    <tr><td style="height: 10px;"></td></tr>
                    <tr>
                        <td colspan="2"><input type="submit" value="Add Item to Cart" name="add_item_to_cart"></td>
                    </tr>
                    <tr><td style="height: 10px;"></td></tr>

                </table>
</body>
</html>

关于jquery - 如何在打开或关闭按钮中使用 Jquery 来限制自动列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42409353/

相关文章:

jquery - 向菜单 <li> 添加一个类,然后使用 jquery 单击链接?

jquery - 收缩的div,然后返回大小,然后翻转jQuery

jquery - 固定一个元素,直到里面的所有东西都被滚动

jquery - 如何使用 jQuery 选择 &lt;input type ="text"> 的值?

javascript - 使用 jQuery 和 css 显示隐藏的 div 时按下 div?

html - 如何消除旋转文本动画中的跳动?

javascript - 在列表项旁边对齐 div

css - div 以 <div style =",,,"> 格式显示,但在放入单独的 css 文件时不显示

javascript - JQuery/javascript 如何获取对象的 ID?

javascript - 为什么包装我的 JS SignalR 回调我的 $(function() 使其不被触发?