简体   繁体   English

如何将JQuery添加到从Ajax调用加载的html中?

[英]How to add JQuery to html loaded from ajax call?

I have a div in a jsp like this : 我在这样的jsp中有一个div:

<div id="response" class="response"></div>

This div, after making an ajax call to a servlet, is appended as : 在对servlet进行ajax调用后,此div附加为:

<div id="response" class="response">
<h3>Connected as user : Tony</h3>
<p>You selected the procedure : <B>lg_resultsretrieval</B></p>
<div class="showbiz">
<label>Enter procedure input variables : </label></div> 
<div class="actual-input">
<label>Parameter1: </label><textarea name='text1' id='text1' class='txtarea' rows='1' cols='50'>starttime - timestamp without time zone</textarea><br> 
<label>Parameter2: </label><textarea name='text2' id='text2' class='txtarea' rows='1' cols='50'>endtime - timestamp without time zone</textarea><br> 
<label>Parameter3: </label><textarea name='text3' id='text3' class='txtarea' rows='1' cols='50'>in_sourceindicator - integer</textarea><br> 
<label>Parameter4: </label><textarea name='text4' id='text4' class='txtarea' rows='1' cols='50'>keyword - character varying</textarea><br> 
</div>
</div>

I wrote a jquery function for the textarea's as follows : 我为textarea编写了一个jquery函数,如下所示:

//submits only when the textbox value is valid
    $("#response").on("change", function() { 
              $("#Execute").click(function () {
                    if ($(".txtarea").val() == this.defaultValue)
                    {
                        alert("Please insert a valid value");
                        return false;
                    }
                });

              $("txtarea")
              .focus(function() {
                    if (this.value === this.defaultValue) {
                        console.log('inside focus function');
                        this.value = '';
                    }
              })
              .blur(function() {
                    if (this.value === '') {
                        this.value = this.defaultValue;
                    }
            });       
            });

Problem is, the above code doesn't work although in the jquery documentation it says to use on function for dynamically loaded elements. 问题是,尽管上面的代码在jquery文档中说要对动态加载的元素使用on函数,但上面的代码不起作用。 How do I make it work ? 我该如何运作? Please suggest 请建议

If the div itself is being replaced in the DOM, then indeed any handler attached to that div will be lost. 如果div本身已在DOM中被替换,则实际上附加到该div任何处理程序都将丢失。 Note how you attach the handler here: 注意如何在此处附加处理程序:

$("#response").on("change", function() {
  // code
});

This attaches the handler directly to the div as it exists when the $("#response") selector is evaluated. 当评估$("#response")选择器时,它会将处理程序直接附加到div Handlers are attached to elements , not to dynamic selectors. 处理程序是附加到element上的 ,而不是附加到动态选择器上的。 In order to retain the handler for elements which are added/replaced, you need to bind to a parent element and filter for the target element. 为了保留添加/替换元素的处理程序,您需要绑定到父元素并为目标元素过滤。 Something like this: 像这样:

$(document).on("change", "#response", function() {
  // code
});

This would bind the handler to the document object, which doesn't change so the handler won't be lost as child elements change. 这会将处理程序绑定到document对象,该对象不会更改,因此处理程序不会随着子元素的更改而丢失。 (For reference, I actually just blogged about this yesterday.) (仅供参考,我昨天实际上只是在写博客 。)

That will not work for dynamically added element.use: 这对于动态添加的element.use无效:

$(document).on("change",'"#response"', function() { 
     //code here
 });

First you have div with id response. 首先,您的div具有ID响应。 then after appending html in this div you can bind all jquery event of added html like this: 然后在此div中添加html之后,您可以像这样绑定所有添加的html的jquery事件:

 $("#response").on(EventName,htmlDom, function() { 
                 //to do your code
      });

eg for textArea Try like this: 例如对于textArea尝试这样:

$("#response").on("focus","textarea", function() { 
         //to do your code
 });

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

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