简体   繁体   English

如何使用 Jquery 自动按下回车键

[英]How to press enter key automatically using Jquery

How can I press enter key without hitting the enter key using Jquery.如何使用 Jquery 在不按 Enter 键的情况下按 Enter 键。 I have a dropdown list.我有一个下拉列表。 after selecting the any value from dropdown, the value of list will copy in input box(search box).从下拉列表中选择任何值后,列表的值将复制到输入框(搜索框)中。 After then I want to call Enter key event automatically using jquery.之后我想使用 jquery 自动调用 Enter 键事件。 Please refer following code.请参考以下代码。

$(document).ready(function(){
                    var data = [
                            {
                                "name":       "Tiger Nixon",
                                "position":   "System Architect",
                                "salary":     "$3,120",
                                "start_date": "2011/04/25",
                                "office":     "Edinburgh",
                                "extn":       "5421"
                            },
                            {
                                "name":       "Black Winters",
                                "position":   "Project Engineer",
                                "salary":     "$1,300",
                                "start_date": "2018/27/05",
                                "office":     "Edinburgh",
                                "extn":       "8412"
                            },
                            {
                                "name":       "Black Winters",
                                "position":   "Project Engineer",
                                "salary":     "$1,300",
                                "start_date": "2018/27/05",
                                "office":     "Edinburgh",
                                "extn":       "8412"
                            },
                            {
                                "name":       "Orange John",
                                "position":   "Network Admin",
                                "salary":     "$3,000",
                                "start_date": "2010/10/12",
                                "office":     "Edinburgh",
                                "extn":       "8425"
                            },
                            {
                                "name":       "Tiger Nixon",
                                "position":   "System Architect",
                                "salary":     "$3,120",
                                "start_date": "2011/04/25",
                                "office":     "Edinburgh",
                                "extn":       "5421"
                            }
                                ];
                    $("#table1").DataTable({
                        data: data,
                                columns: [
                                    { data: 'name' },
                                    { data: 'position' },
                                    { data: 'salary' },
                                    { data: 'start_date' },
                                    { data: 'office' },
                                    { data: 'extn' }
                                ]
                    });

                    /* color of data table */
                    var seen = {};
                    $('#table1 tr').each(function() {
                    var txt = $(this).text();
                    if (seen[txt])
                        $(this).css("background-color", "red");
                    else
                        seen[txt] = true;
                    });
                    var positionSet = new Set();
                    positionSet.add("System Architect");
                    positionSet.add("Project Engineer");
                    positionSet.add("Network Admin");

                    for(let myPositionSet of positionSet){
                        console.log(myPositionSet);
                        $('#empPosition').append($('<option>', { 
                            value: myPositionSet,
                            text : myPositionSet 
                        }));
                    }
                    $("#empPosition").change(function() {
                        $("#table1_filter input").val(this.value);
                        e = jQuery.Event("change")
                        e.which = 13 //choose the one you want
                            $("#table1_filter input").change(function(){
                             alert('keypress triggered')
                            }).trigger(e)
                    })

                });

Here is the HTML of this above code这是上述代码的 HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
 <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.js"></script>
 <select id="empPosition">
                </select>
            <table id="table1" class="display">
                    <thead>
                        <tr>
                            <th>name</th>
                            <th>position</th>
                            <th>salary</th>
                            <th>start_date</th>
                            <th>office</th>
                            <th>extn</th>
                        </tr>
                    </thead>
                </table>

The code is not working as i want.代码没有按我的意愿工作。 After copying the value in search box It should hit the enter and then it will show the result that i have copy in search box.在搜索框中复制值后它应该按回车键,然后它将显示我在搜索框中复制的结果。

Try keyup event instead of change event尝试keyup事件而不是change事件

$("#table1_filter input").val(this.value).trigger('keyup')

Try keypress or keydown event:尝试 keypress 或 keydown 事件:

var keyEvent = $.Event("keydown");
keyEvent.which = 13; 
$("#table1_filter input").trigger(keyEvent);

Link to know keyEvent value: https://keycode.info/了解keyEvent值的链接: https://keycode.info/

I think you should consider search API in Datatable instead of enter event我认为您应该考虑在 Datatable 中搜索 API 而不是enter事件

var dataTable = $("#table1").DataTable({
        data: data,
        columns: [
          { data: 'name' },
          { data: 'position' },
          { data: 'salary' },
          { data: 'start_date' },
          { data: 'office' },
          { data: 'extn' }
        ]
      });

$("#empPosition").change(function (event) {
        $("#table1_filter input").val(this.value);
        dataTable.search( this.value ).draw();
})

https://datatables.net/reference/api/search() https://datatables.net/reference/api/search()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM