jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了未捕获的TypeError:undefined在使用jQuery时不是函数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我写了这个脚本来向我的机器发出TCPIP请求,这很好用.

当我在开关中获得status = 0(0 =读卡器未准备好)时,我编写了代码,以便10秒后的间隔再次“按下”“#print_card”按钮,以便它再次尝试请求.

一切正常,但当我再次按下按钮时使用$(‘#print_card’).触发(“点击”);我得到了一个着名的错误

未捕获的TypeError:undefined在使用jQuery时不是函数,控制台将此函数提供给bootstrap.min.js:6文件(这是原始的Bootstrap文件).

我试过$(‘#print_card’).click();并没有任何变化……

这是我的代码.如果我删除第439行#(‘#print_card’).触发(“点击”);我没有得到错误……:

var interval;
  var status_timer;
  function attendi_e_riprova(txt){
    var sec = 10;

    var interval = setInterval(function(){
      if(sec != 1){
        sec--;
        if(sec == 1){
          sec_word = 'secondo';
        }else{
          sec_word = 'secondi';
        }
        $('#card-modal-body').html('<p>'+txt+'</p>');
      }else{
        clearInterval(interval);

        //THIS IS THE TRIGGER <-----------------------------------
        $('#print_card').trigger("click");
        //THIS IS THE TRIGGER <-----------------------------------

        return false;
      }
    },1000);

    $('.close').click(function(){
      clearInterval(interval);
      clearInterval(status_timer);         
    }); 

  }

  var codemaster = '1';
  var chain = 1;
  var shop = 1;

  function scriviCard(){
    //Another function
  }


  $('#print_card').click(function(){
    $('#card-modal-body').html('<p><i class="fa fa-spinner fa-spin"></i> Ricerca del lettore e card,attendere&hellip;</p>');

    var req = '"CWD2";"';

    $.post(base_url()+'config/RichiestaTCPIP',{array:req},function(status){
      if(status == 'err'){
        console.error('Errore: controller file config.PHP funzione di linea #117');
      }else{
        console.log('Status: '+status)

        switch(status){
          case '0':
            attendi_e_riprova('<i class="fa fa-ban text-danger"></i> Lettore NON disponibile. Assicurarsi che sia collegato.');
          break;
          case '1':
            attendi_e_riprova('Lettore vuoto. Inserire una card vergine per poterla scrivere.');
          break;
          case '4':
            attendi_e_riprova('<i class="fa fa-check text-success"></i> Card trovata. In attesa di riconoscimento...');
          break;
          case '5':
            attendi_e_riprova('<i class="fa fa-ban text-danger"></i> Card in scrittura. Attendere completamento.');
          break;
          default:
            //Nuova richiesta per la creazione card (per status 2 o 3)
            scriviCard();
            //console.log(status)
          break;
        }
      }         
    })

  })

解决方法

我认为你应该尝试在触发命令之前在变量中定义你的函数!这样的事情:

window.clickFunction=function(
    $('#card-modal-body').html('<p><i class="fa fa-spinner fa-spin"></i> Ricerca del lettore e card,attendere&hellip;</p>');

var req = '"CWD2";"';

$.post(base_url()+'config/RichiestaTCPIP',function(status){
  if(status == 'err'){
    console.error('Errore: controller file config.PHP funzione di linea #117');
  }else{
    console.log('Status: '+status)

    switch(status){
      case '0':
        attendi_e_riprova('<i class="fa fa-ban text-danger"></i> Lettore NON disponibile. Assicurarsi che sia collegato.');
      break;
      case '1':
        attendi_e_riprova('Lettore vuoto. Inserire una card vergine per poterla scrivere.');
      break;
      case '4':
        attendi_e_riprova('<i class="fa fa-check text-success"></i> Card trovata. In attesa di riconoscimento...');
      break;
      case '5':
        attendi_e_riprova('<i class="fa fa-ban text-danger"></i> Card in scrittura. Attendere completamento.');
      break;
      default:
        //Nuova richiesta per la creazione card (per status 2 o 3)
        scriviCard();
        //console.log(status)
      break;
    }
  }});

然后当你想要调用它时使用它:

$('#print_card').click(window.clickFunction);

要么

window.clickFunction.trigger();

注意:您必须将变量定义为全局变量,并且必须在触发器或单击行之前定义!

让我知道事情的后续.

大佬总结

以上是大佬教程为你收集整理的未捕获的TypeError:undefined在使用jQuery时不是函数全部内容,希望文章能够帮你解决未捕获的TypeError:undefined在使用jQuery时不是函数所遇到的程序开发问题。

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

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