Home > Mobile >  I am get error jquery code bubbling method
I am get error jquery code bubbling method

Time:05-25

I'm trying to make an application that includes input, paste and cut events. I have a problem. While paste, the input event also works. I want to close one. the code should not run twice. I applied abc but it didn't work. please help. when i run the code i get results twice.

My code is below,

let authorOrcid = $("#article_author_submission_author_orcid");

authorOrcid.bind('input paste cut', function (e) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        let orcidInputNumberValue = $(this).val().replace(/[^0-9\.]/g, '');
        if (orcidInputNumberValue.length > 0) {
          helpBlock.removeClass("d-none");
          if (orcidInputNumberValue.length === 16) {
            let orcidInputFullValue = $(this).val();
            $.post("{{ path('ojs_user_check_orcid') }}", {orcid: orcidInputFullValue}, function (result) {
              if (result !== null) {
                if (result !== false) {
                  console.log("murat");
                    authorOrcid.removeClass("is-invalid");
                    helpBlock.removeClass("invalid-feedback");
                    authorOrcid.addClass("is-valid");
                    helpBlock.addClass("valid-feedback").text("{{ 'valid.orcid'|trans }}");
                  }else{
                  console.log("mkoc");
                  helpBlock.addClass("invalid-feedback").text("{{ 'invalid.orcid'|trans }}");
                  helpBlock.removeClass("valid-feedback");
                  authorOrcid.addClass("is-invalid");
                  orcidDescBlock.css("margin-top", "-7px");
                }
              }
            });
          } else {
            console.log("sivas");
            helpBlock.addClass("invalid-feedback").text("{{ 'invalid.orcid'|trans }}");
            helpBlock.removeClass("valid-feedback");
            authorOrcid.addClass("is-invalid");
            orcidDescBlock.css("margin-top", "-7px");
          }
        } else {
          console.log("ync");
          helpBlock.addClass("d-none");
          authorOrcid.removeClass("is-invalid");
          authorOrcid.removeClass("is-valid");
          authorOrcid.removeClass("invalid");
          helpBlock.removeClass("invalid-feedback");
        }
      })

CodePudding user response:

The cut and paste are also considered as input events so the code is gonna run twice if you don't set e.preventDefault() right after the first run.

let authorOrcid = $("#article_author_submission_author_orcid");

      authorOrcid.bind("input paste cut", function (e) {
        if (e.type == "input") {
          console.log(e.type);
        } else if (e.type == "cut") {
          console.log(e.type);
        } else {
          console.log(e.type);
        }
        e.preventDefault();
      });

CodePudding user response:

This method worked

et authorOrcid = $("#article_author_submission_author_orcid");

  authorOrcid.bind("input paste cut", function (e) {
     if(e.type === "paste") {
     return false;
   }
  });
  • Related