简体   繁体   English

在事件处理上将 javascript 转换为 jquery

[英]Convert javascript to jquery on event handling

I have the following code:我有以下代码:

    input.setOnkeypress("if (event.keyCode == 13) 
                {
                document.getElementById('search_report_form:search_button').onclick(); 
                return true;
                }");

And this is the function:这是功能:

final class GroupsSelector extends BaseRenderablePanel<GroupsSelector> {
        private GroupsSelector group(LabourInputReportCriteria.Level level) {
            HtmlSelectBooleanCheckbox box = new HtmlSelectBooleanCheckbox();
            boolean isSelected = selections.isGroupSelected(level);
            box.setSelected(isSelected);
            // box.setDisabled(isDaySelectedOnFirst(level));
            box.setId("groupBy" + level.getClass().getSimpleName());
            box.setOnclick("submit()");
            box.addValueChangeListener(u.addExpressionValueChangeListener("#{reportSearchCriteriaModel.groupBy}"));
            HtmlOutputText labelComponent = new HtmlOutputText();

            labelComponent.setValue(getGroupSelectionValue(level));
            tr().td();
            html(box);
            html("&nbsp;");
            html(labelComponent);
            endTd().endTr();
            return this;
        }

First off, you don't need to change it to jQuery if it's working.首先,如果它可以工作,您不需要将其更改为 jQuery。 Adding jQuery to your page doesn't mean the DOM stops working.将 jQuery 添加到您的页面并不意味着 DOM 停止工作。 And if it's not working, changing your DOM code to equivalent jQuery code won't make it start working.如果它工作,改变你的DOM代码,相当于jQuery代码不会让它开始工作。

But the jQuery equivalent of但是jQuery相当于

document.getElementById('search_report_form:search_button').onclick();

is

$('#search_report_form\\3a search_button').click();

or more readably;或更易读;

$('[id="search_report_form:search_button"]').click();

The tricky bit here is that you have a colon ( : ) in the id , so we can't just do #search_report_form:search_button to look it up, because the colon looks like it's the beginning of a pseudo-class.这里棘手的一点是,你有一个冒号( : )的id ,所以我们不能只是做#search_report_form:search_button来看看它,因为结肠看起来就像是一个伪类的开始。 So we have to escape it.所以我们必须逃避它。 In CSS selectors, you escape it by replacing it with a backslash followed by its hex equivalent.在 CSS 选择器中,您可以通过将其替换为反斜杠后跟其等效的十六进制来对其进行转义。 The character code for : in hex is 3A, so \\3a . :在十六进制中的字符代码是 3A,所以\\3a To write a backslash in a string literal, you have to write two of them (the first escapes the second).要在字符串文字中写入反斜杠,您必须编写其中的两个(第一个转义第二个)。 You need the space after it to terminate it, hence '#search_report_form\\\\3a search_button' .您需要它后面的空格来终止它,因此'#search_report_form\\\\3a search_button'

The second form uses an attribute selector instead, since we can put the ID in quotes, and don't have to worry about the colon.第二种形式使用属性选择器代替,因为我们可以将 ID 放在引号中,而不必担心冒号。

If you want to convert your first few lines to JQuery then Try this如果你想将你的前几行转换为 JQuery 那么试试这个

$('input').on('keypress', function(event){
    if (event.keyCode == 13) {
       $('[id="search_report_form:search_button"]').click(); //.submit();
       return true;
    }
});

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

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