jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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 = $('<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>

解决方法

你的代码……

jQuery的:

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

HTML:

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

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

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

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

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

See the documentation for .on().

大佬总结

以上是大佬教程为你收集整理的jquery – 为什么我的.on(‘更改’)不起作用?全部内容,希望文章能够帮你解决jquery – 为什么我的.on(‘更改’)不起作用?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。