简体   繁体   English

无法跟踪输入值的变化

[英]Not able to track change in input value

In the template I have a input element. 在模板中,我有一个输入元素。

<input class='qq-edit-caption-selector qq-edit-caption kk-editing' placeholder='Enter Caption here ...' onkeypress='captionUpdate();'>

I want to track the change in the input value,and enable the Update button.Tried below options: 我想跟踪输入值的变化,并启用“更新”按钮。尝试以下选项:

  • onkeypress='captionUpdate();'
  • Tried jquery change or click on the class 尝试更改jQuery或单击类
$('.qq-edit-caption').change(function() {
    alert('qq-edit-caption');        
});

Both options does not get fired up!!not sure I have anything issues with my setup or Fine Uploader does not allow that? 这两个选项都无法启动!不确定我的设置是否存在任何问题,或者Fine Uploader不允许这样做吗? Please see my screenshot : 请看我的截图

Any way to solve this problem with FU? 有什么办法可以解决FU吗?

This might get you started: 这可能会让您入门:

 $('.inp input').keyup(function(){ if (this.value.length > 0) { $(this).closest('.row').find('.cell.btn button.upload').prop('disabled', false); }else{ $(this).closest('.row').find('.cell.btn button.upload').prop('disabled', true); } }); 
 * {position:relative;box-sizing:border-box;} .row{overflow:hidden;} .cell{float:left;height:40px;} .pic{width:82px;} .inp{width:230px;} .inp input{font-size:1rem;padding:2px 5px;} .btn{width:60px;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="row"> <div class="cell pic"> <img src="http://lorempixel.com/80/40"> </div> <div class="cell inp"> <input class='qq-edit-caption-selector qq-edit-caption kk-editing' placeholder='Enter Caption...'> </div> <div class="cell btn"> <button class="upload" disabled>Upload</button> </div> <div class="cell btn"> <button class="del" disabled>Delete</button> </div> </div><!-- .row --> 

If you are simply adding this inline event handler directly to the template element, then it's not surprising that it's never triggered. 如果您只是将此内联事件处理程序直接添加到template元素,那么它永远不会触发也就不足为奇了。 Fine Uploader templates are quite primitive in that the template is interpreted as an HTML string and then used to create DOM elements inside of your container element (the element referenced as your element option ). Fine Uploader模板非常原始,因为该模板被解释为HTML字符串,然后用于在容器元素(称为element option的element )内创建DOM元素。

You really should never use inline event handlers. 您绝对不应该使用内联事件处理程序。 There are quite a few disadvantages to this approach. 这种方法有很多缺点。 I talk about this in more depth in my book - Beyond jQuery . 我将在《 超越jQuery》一书中更深入地讨论这一点。 And the method of attaching event handlers is not necessary at all in your case, as far as I can tell. 据我所知,在您的情况下,完全不需要附加事件处理程序的方法。 Instead, after constructing a new instance of Fine Uploader, simply attach an event handler of your choice to the input element using addEventListener . 相反,在构造Fine Uploader的新实例之后,只需使用addEventListener将您选择的事件处理程序附加到输入元素即可。 For example if your <input> element is given a CSS class name of 'qq-edit-caption', you can attach a "change" event handler like this: 例如,如果您的<input>元素的CSS类名称为'qq-edit-caption',则可以附加一个“ change”事件处理程序,如下所示:

var uploadContainer = document.querySelector('#my-uploader')

var uploader = new qq.FineUploader({
   element: uploadContainer
   ...
})

uploadContainer.querySelector('.qq-edit-caption')
  .addEventListener('change', function(event) {
      // event handler logic here...
   })

...and if you are creating this input element for each file and need to attach a "change" handler to all of these input elements, you should attach a single delegated event handler to the container element, and react based on the element that initially triggered the event (look at the target property of the event). ...并且如果您要为每个文件创建此输入元素,并且需要在所有这些输入元素上附加一个“更改”处理程序,则应在容器元素上附加一个委托的事件处理程序,并根据该元素进行反应最初触发了事件(请查看事件的target属性)。 You can determine the ID of the file by looking at the CSS class of the parent <li> of the event.target , or you can look for a 'qq-file-id' attribute on the parent <li> of the target element (the value will be the file ID). 您可以通过查看event.target的父<li>的CSS类来确定文件的ID,或者可以在目标元素的父<li>上查找'qq-file-id'属性(该值将是文件ID)。 That code might look something like this: 该代码可能看起来像这样:

var uploadContainer = document.querySelector('#my-uploader')

var uploader = new qq.FineUploader({
   element: uploadContainer
   ...
})

uploadContainer.addEventListener('change', function(event) {
      if (event.target.className.indexOf('qq-edit-caption') >= 0) {
         var fileId = parseInt(event.target.getAttribute('qq-file-id'))
         // ...
      }
   })

You can enable and disable the state of button by the input value.Using the Keyup function 您可以通过输入值启用和禁用按钮的状态。使用Keyup功能

 $('.qq-edit-caption').keyup(function() {
       if(this.value.length > 0){
            $("#edit").prop('disabled', false);
       }
       else {
        $("#edit").prop('disabled', true);
       }
    });

Have you tried the onchange event? 您是否尝试过onchange事件? Does it work? 它行得通吗?

<input class='qq-edit-caption-selector qq-edit-caption kk-editing' placeholder='Enter Caption here ...' onchange='captionUpdate();'>

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

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