简体   繁体   English

从ajax请求中单击按钮时的jQuery功能

[英]jquery function on button click from ajax request

So im building this page where i am including a file input using ajax, but i am not able to trigger jquery when a the file is changed. 因此,即时通讯建立此页面,我在其中包含使用Ajax输入的文件,但是当文件更改时,我无法触发jquery。 is this a normal thing or should this just work? 这是正常现象还是应该起作用? I am trying to get the filename displayed in the input type text field. 我正在尝试在输入类型文本字段中显示文件名。

My ajax call 我的ajax电话

$('.wijzigproduct').on('click', function () {
    var productvalue = $(this).val();
    $.ajax({
        url: "includes/productwijzigen.php?q=" + productvalue,
        success: function (result) {
            $('#editproduct').html(result);
        }
    });
});

My input fields: 我的输入字段:

<div class="input-group">
                <span class="input-group-btn">
                    <span class="btn btn-default btn-file">
                        Bladeren… <input type="file" name="imgInpnew" id="imgInpnew">
                    </span>
                </span>
                <input type="text" class="form-control" id='imgOutpnew' readonly>
            </div>
            <img id='imgshownew'/>

My jquery: 我的jQuery:

$('#imgInpnew').change(function () {
            var filename = $('#imgInpnew').val();
            filename = filename.substring(filename.lastIndexOf("\\") + 1, filename.length);
            $('#imgOutpnew').val(filename);
        });

The change() binding you're using is called a "direct" binding which will only attach the handler to elements that already exist. 您正在使用的change()绑定称为“直接”绑定,它将仅将处理程序附加到已经存在的元素上。 It won't get bound to elements created in the future. 它不会绑定到将来创建的元素。

Since you have generated DOM using jQuery, you have to create a "delegated" binding by using on() . 由于您已经使用jQuery生成了DOM,因此必须使用on()创建一个“委托”绑定。 Here is the solution base on the code you have provide on jsfiddle.net/a70svxto 这是基于您在jsfiddle.net/a70svxto上提供的代码的解决方案

 $.ajax({ url: "/echo/js/?js=<div class=\\"input-group\\"><span class=\\"input-group-btn\\"><span class=\\"btn btn-default btn-file\\">search… <input type=\\"file\\" name=\\"imgInpnew\\" id=\\"imgInpnew\\"></span></span><input type=\\"text\\" class=\\"form-control\\" id='imgOutpnew' readonly></div><img id='imgshownew\\'/>", success: function(result) { $('#div').html(result); } }); $('#div').on('change', '#imgInpnew', function() { var filename = $('#imgInpnew').val(); filename = filename.substring(filename.lastIndexOf("\\\\") + 1, filename.length); $('#imgOutpnew').val(filename); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='div'></div> 

well you are attaching the change event to a not existing element in the DOM. 很好,您将change事件附加到DOM中不存在的元素上。

you have to first add the element into the DOM and then attach the event to the element 您必须先将元素添加到DOM中,然后将事件附加到元素

$.ajax({
  url: "/echo/js/?js=<div class=\"input-group\"><span class=\"input-group-btn\"><span class=\"btn btn-default btn-file\">search… <input type=\"file\" name=\"imgInpnew\" id=\"imgInpnew\"></span></span><input type=\"text\" class=\"form-control\" id='imgOutpnew' readonly></div><img id='imgshownew\'/>",
  success: function(result) {
    $('#div').html(result);
    $('#imgInpnew').change(function() {
  var filename = $('#imgInpnew').val();
  filename = filename.substring(filename.lastIndexOf("\\") + 1, filename.length);
  $('#imgOutpnew').val(filename);
});
  }
});

https://jsfiddle.net/a70svxto/ https://jsfiddle.net/a70svxto/

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

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