Jquery:如何使用onload事件动态绑定文本框?

时间:2022-01-08 08:58:55

A textbox is created on runtime in Javascript. It gets open when user clicks a button.

在Javascript中在运行时创建文本框。当用户单击按钮时它会打开。

<input type="text" id="documentTitle" name="documentTitle" value="<spring:message code="document.Title"/>"

On click I want to display textbox text highlighted.

点击我想显示突出显示的文本框文本。

How to fire onload element using JQuery?

如何使用JQuery触发onload元素?

Tried following JQuery, but not successful:

尝试遵循JQuery,但没有成功:

$(document).on("load", "#documentTitle" , function() {
        myquery.highlightText(this);
});

2 个解决方案

#1


1  

i don't what you exactly want but here some code that may help you ?

我不是你想要的,但这里有一些可能对你有帮助的代码?

$(document).ready(function() {
  alert("on load event fire");
  $("button").click(function() {
    $("textarea").show();
  })
});
textarea {
  display: block;
  width: 400px;
  color: black;
  padding: 10px;
  text-shadow: 0px 0px 2px rgba(255, 0, 0, 1);
  height: 120px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea style="display:none">asdasdasd</textarea>
<button>
  show
</button>

#2


1  

You dont have the choice there is no event fired when an arbitrary DOM element such as a <div> becomes ready.

您没有选择当任何DOM元素(如

)准备就绪时不会触发事件。

    $(document).ready(function(e) {
        $(document).on("click", "#test", function() {
            $("#content").append('<input type="text" id="documentTitle" name="documentTitle" value=""/>');
            
            //do your highlight here
            $("#content #documentTitle").val("test");
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="test" value="test" />
<div id="content"></div>

If you really want an event you should create a custom one

如果你真的想要一个活动,你应该创建一个自定义活动

$(document).ready(function(e) {
    $(document).on("click", "#test", function() {
        $("#content").append('<input type="text" id="documentTitle" name="documentTitle" value=""/>');           
        $("#content #documentTitle").trigger("myLoadedevent");
    });

    $(document).on("myLoadedevent", "#documentTitle", function() {
        //do your highlight here
        alert('event');
    });

});

#1


1  

i don't what you exactly want but here some code that may help you ?

我不是你想要的,但这里有一些可能对你有帮助的代码?

$(document).ready(function() {
  alert("on load event fire");
  $("button").click(function() {
    $("textarea").show();
  })
});
textarea {
  display: block;
  width: 400px;
  color: black;
  padding: 10px;
  text-shadow: 0px 0px 2px rgba(255, 0, 0, 1);
  height: 120px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea style="display:none">asdasdasd</textarea>
<button>
  show
</button>

#2


1  

You dont have the choice there is no event fired when an arbitrary DOM element such as a <div> becomes ready.

您没有选择当任何DOM元素(如

)准备就绪时不会触发事件。

    $(document).ready(function(e) {
        $(document).on("click", "#test", function() {
            $("#content").append('<input type="text" id="documentTitle" name="documentTitle" value=""/>');
            
            //do your highlight here
            $("#content #documentTitle").val("test");
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="test" value="test" />
<div id="content"></div>

If you really want an event you should create a custom one

如果你真的想要一个活动,你应该创建一个自定义活动

$(document).ready(function(e) {
    $(document).on("click", "#test", function() {
        $("#content").append('<input type="text" id="documentTitle" name="documentTitle" value=""/>');           
        $("#content #documentTitle").trigger("myLoadedevent");
    });

    $(document).on("myLoadedevent", "#documentTitle", function() {
        //do your highlight here
        alert('event');
    });

});