点击按钮进入按JQuery

时间:2017-07-11 11:12:25

标签: javascript jquery

  
  $("#AddDataStavka, #AddDataRazmer").on("keyup", function (event) {
    if (event.keyCode == 13) {
        e.preventDefault();
        $("tr.trNewLine").children().first().children().first().get(0).click();
    }
});

/*This is code inside a document.ready, what I'm trying to do is call this every time a button is pressed and if its the enter key it should click the button.
What am I doing wrong?

Here's what I'm trying to target:*/
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

            <tr runat="server" id="trNewLine" class="trNewLine" visible="false">
                <td runat="server" id="operationCol3">
                    <asp:LinkButton ID="btnAddDataAdd" runat="server" class="btn btn-pireus" OnClick="btnAddAddData_Click" ToolTip="Добави застраховка"><span>Добавяне</span></asp:LinkButton></td>
                <td>
                    <asp:DropDownList runat="server" ID="AddDataType" class="DropDownListHint" data-taggle="dropdown" data-style="DropDownListHint-datastyle" OnSelectedIndexChanged="AddDataType_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                </td>
                <td>
                    <input type="text" class="form-control form-control-pireus" id="AddDataStavka" clientidmode="static" onchange="glowInsuranceCheck()" runat="server" maxlength="15" text=''>
                </td>
                <td>
                    <select class="DropDownListHint" id="AddDataRisk" runat="server" data-placement="top" data-taggle="dropdown" data-style="DropDownListHint-datastyle"></select>
                </td>
                <td>
                    <input type="text" class="form-control form-control-pireus bst-suma" id="AddDataRazmer" clientidmode="static" onchange="glowInsuranceCheck()" runat="server" maxlength="19">
                </td>
                <td>
                    <select class="DropDownListHint" id="AddDataInsurer" runat="server" data-placement="top" data-taggle="dropdown" data-style="DropDownListHint-datastyle"></select>
                </td>
                <td>
                    <select class="DropDownListHint" id="AddDataInsured" runat="server" data-placement="top" data-taggle="dropdown" data-style="DropDownListHint-datastyle"></select>
                </td>
            </tr>

3 个答案:

答案 0 :(得分:1)

根据您的事件处理函数,e.preventDefault使用event而不是

  
  $("#AddDataStavka, #AddDataRazmer").on("keyup", function (event) {
    if (event.keyCode == 13) {
        event.preventDefault();
        $("tr.trNewLine").children().first().children().first().focus().trigger("click");
    }
});

/*This is code inside a document.ready, what I'm trying to do is call this every time a button is pressed and if its the enter key it should click the button.
What am I doing wrong?

Here's what I'm trying to target:*/
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

            <tr runat="server" id="trNewLine" class="trNewLine" visible="false">
                <td runat="server" id="operationCol3">
                    <asp:LinkButton ID="btnAddDataAdd" runat="server" class="btn btn-pireus" OnClick="btnAddAddData_Click" ToolTip="Добави застраховка"><span>Добавяне</span></asp:LinkButton></td>
                <td>
                    <asp:DropDownList runat="server" ID="AddDataType" class="DropDownListHint" data-taggle="dropdown" data-style="DropDownListHint-datastyle" OnSelectedIndexChanged="AddDataType_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                </td>
                <td>
                    <input type="text" class="form-control form-control-pireus" id="AddDataStavka" clientidmode="static" onchange="glowInsuranceCheck()" runat="server" maxlength="15" text=''>
                </td>
                <td>
                    <select class="DropDownListHint" id="AddDataRisk" runat="server" data-placement="top" data-taggle="dropdown" data-style="DropDownListHint-datastyle"></select>
                </td>
                <td>
                    <input type="text" class="form-control form-control-pireus bst-suma" id="AddDataRazmer" clientidmode="static" onchange="glowInsuranceCheck()" runat="server" maxlength="19">
                </td>
                <td>
                    <select class="DropDownListHint" id="AddDataInsurer" runat="server" data-placement="top" data-taggle="dropdown" data-style="DropDownListHint-datastyle"></select>
                </td>
                <td>
                    <select class="DropDownListHint" id="AddDataInsured" runat="server" data-placement="top" data-taggle="dropdown" data-style="DropDownListHint-datastyle"></select>
                </td>
            </tr>

答案 1 :(得分:1)

请改用以下代码。在元素上使用keydown/keypress事件。

$('body').on("#AddDataStavka, #AddDataRazmer", "keydown", function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            $("tr.trNewLine").children().first().children().first().get(0).click();
        }
    });

答案 2 :(得分:0)

尝试直接选择#btnAddDataAdd:

$("#AddDataStavka, #AddDataRazmer").keypress(function (e){
  if (e.which === 13) {
      e.preventDefault();
      $("#btnAddDataAdd"").click();
  };
})