微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – 为什么我的.on(‘更改’)不起作用?

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

我正在尝试将更改事件附加到type =“file”输入,但它只是不起作用.

我试过两种方法,第一种:

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

其中输入有一个文件输入隐藏类.

第二个:

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

但是当使用输入选择/更改文件时,既不会激活该功能.

出了什么问题?

编辑:

在这里查看页面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 = $('browse = $('browse').attr('id','file'+thisnumber).text('browse');
            var fileclear = $('browse,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:

    
browse" id="file1">browse
最佳答案
你的代码……

jQuery的:

$('.container').on('change',function(){

HTML:

它不起作用,因为输入[type =“file”]不在.container中.

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

$(document).on('change',function(){

See the documentation for .on().

原文地址:https://www.jb51.cc/jquery/428670.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐