【问题标题】:why is my .on('change') not working?为什么我的 .on('change') 不起作用?
【发布时间】:2013-01-25 20:31:01
【问题描述】:

我正在将输入动态添加到我的页面中,因此需要使用.on 将事件附加到它们。

我正在尝试将 change 事件附加到 type="file" input,但它不起作用。

我尝试了两种方法,第一种:

$('.container').on('change', '.file-input-hidden' , function(){

输入的类为file-input-hidden

第二个:

$('.container').on('change', 'input[type="file"]' , function(){

但当使用input 选择/更改文件时,都不会触发该函数。

怎么了?

编辑:

在此处查看页面:LINK

和js:

$('.container').on('click', '.file-browse' , function(){
    var thisone = $(this).attr('id');
    $('input[name="input-'+ thisone+'"]').click();
});

$('.file-input-hidden').on('change', function(){
    var thetext = $(this).val();
    var thetextsplit = thetext.split('\\').pop();
    var thisone = $(this).attr('name').split('-').pop();
    if($('.file-info').text() == thetextsplit){
        alert('you have already selected this file');
        $(this).replaceWith( $(this).val('').clone( true ) );
    }
    else{
        $('#info-'+ thisone).text(thetextsplit);
        $('#clear-'+ thisone).fadeIn(100);
        var emptyinputs = $('.file-info:empty').length;
        if(emptyinputs <1){
            var filledinputs = $(".file-info:contains('.')").length;
            var thisnumber = filledinputs + 1;
            var filecontainer = $('<div>').addClass('file-container');
            var fileinfo = $('<div>').addClass('file-info').attr('id','info-file'+thisnumber);
            var filebrowse = $('<div>').addClass('file-browse').attr('id','file'+thisnumber).text('Browse');
            var fileclear = $('<div>').addClass('file-clear').attr('id','clear-file'+thisnumber).text('X');
            var newinput = $('<input>').attr({'type':'file','name':'input-file'+thisnumber}).addClass('file-input-hidden');
            var thebody = $('.container');
            (filecontainer).append(fileinfo,filebrowse,fileclear);
            filecontainer.appendTo(thebody);

            var theform = $('#hidden-inputs');
            newinput.appendTo(theform);
        }
    }

    if($(this).val() == ''){
    $('#clear-'+thisone).fadeOut(100);
    }
});

$('.container').on('click', '.file-clear' , function(){
    var thisone = $(this).attr('id').split('-').pop();
    $('input[name="input'+ thisone +'"]').replaceWith( $('input[name="input'+ thisone +'"]').val('').clone( true ) );
    $('#info-'+ thisone).text('');
    $(this).fadeOut(100);
});

HTML:

    <div class="container">
    <div class="file-container">
      <div class="file-info" id="info-file1"></div>
      <div class="file-browse" id="file1">Browse</div>
      <div class="file-clear" id="clear-file1">X</div>
    </div>
    </div>

    <form action="" method="POST" enctype="multipart/form-data" id="hidden-inputs">
  <input type="submit" style="clear:both; float:left;"/>
  <input type='file' name="input-file1" class="file-input-hidden" />
    </form>

【问题讨论】:

  • 向我们展示一个完整简洁示例的足够代码。
  • $('.file-input-hidden').on('change' function() { }); 工作吗?
  • 你有 jquery 库的引用吗?
  • 确实如此,但仅适用于第一个元素(始终在页面上)。任何动态添加的元素都不起作用

标签: jquery jquery-on


【解决方案1】:

您的代码...

jQuery

$('.container').on('change', 'input[type="file"]' , function(){

HTML

<div class="container">...</div>

<form>
    <input type='file' name="input-file1" class="file-input-hidden" />
</form>

它不起作用,因为input[type="file"] 不在.container内部

把它放在div.container 里面或者试试这样的东西..

$(document).on('change', 'input[type="file"]' , function(){

See the documentation for .on().

【讨论】:

  • 是的,不客气。现在您明白了为什么显示 HTML 与 jQuery 问题相关。 ;-)
【解决方案2】:

如果你在页面加载后添加元素,你必须在生成新输入的代码之后再次调用 change 方法,你可以这样使用:

$('.file-input-hidden').on("change", function(){

})

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-06-04
  • 2016-06-04
  • 2018-12-17
  • 2014-09-25
  • 2017-07-11
  • 1970-01-01
  • 2021-05-31
  • 1970-01-01
相关资源
最近更新 更多