4

I use jquery in my phtml frontend page like this:

<script>
  require(['jquery', 'jquery/ui'], function($){
     var currentUrl = '<?= $itemEditUrl  ?>';

     $('#link-cusom-campaign').click( function(e) {
       e.preventDefault();
       var check = 1;
       var optionsInput = '';
       $('select[name^="options"]').each(function() {
           if($(this).val() == ''){
              check = 0;
              $("<p style='color:red;' class='alert-empty'>This is a required field</p>").insertAfter(this).delay(3000).fadeOut();
           } else {
             var name = $(this).attr('name');
             var id = parseInt(name.match(/[0-9]+/));
             optionsInput += '<input type="text" name="options['+id+']" value="'+$(this).val()+'"/> ';
           }
       });
       if(check == 1){
          var model = $('#select_18').val();
          var qty = $('#qty').val();
          var getUrl = currentUrl + '?model=' +model+'&qty=' + qty;
          var form = $('<form  action="' + currentUrl + '" method="post">' +
            optionsInput +
            '<input type="text" name="qty" value="' + qty + '" />' +
            '</form>');
          $('body').append(form);
          form.submit();
       }
     });
  });
</script>

all the function work properly, but in my browser console i got an error pointing at require saying :

VM4522:2 Uncaught ReferenceError: require is not defined
5
  • Which line it's pointing to when you clicked to VM4522:2 Uncaught ReferenceError: require is not defined? And did you clear the static content, redeploy it after your changes were made? May 4, 2017 at 8:02
  • @ToanNguyen the 2nd line require(['jquery', 'jquery/ui'], function($){, ive dont static content deploy numerous time
    – Hunter
    May 4, 2017 at 8:04
  • Please execute bin/magento setup:upgrade (to remove static, generation, someother temporary files), then bin/magento setup:static-content:deploy and see if this error still happens. May 4, 2017 at 8:06
  • check this github.com/magento/magento2/issues/1321 May 4, 2017 at 8:08
  • @QaisarSatti: Not that issue, obviously. May 4, 2017 at 8:35

1 Answer 1

0

Try something like this

requirejs(['require'], function (require) {
    require(['jquery', 'jquery/ui'], function ($) {
        var currentUrl = '<?= $itemEditUrl ?>';

        $('#link-cusom-campaign').click(function (e) {
            e.preventDefault();
            var check = 1;
            var optionsInput = '';
            $('select[name^="options"]').each(function () {
                if ($(this).val() == '') {
                    check = 0;
                    $("<p style='color:red;' class='alert-empty'>This is a required field</p>").insertAfter(this).delay(3000).fadeOut();
                } else {
                    var name = $(this).attr('name');
                    var id = parseInt(name.match(/[0-9]+/));
                    optionsInput += '<input type="text" name="options[' + id + ']" value="' + $(this).val() + '"/> ';
                }
            });
            if (check == 1) {
                var model = $('#select_18').val();
                var qty = $('#qty').val();
                var getUrl = currentUrl + '?model=' + model + '&qty=' + qty;
                var form = $('<form  action="' + currentUrl + '" method="post">' +
                        optionsInput +
                        '<input type="text" name="qty" value="' + qty + '" />' +
                        '</form>');
                $('body').append(form);
                form.submit();
            }
        });
    });
});
1
  • It doesn't seem right for Require JS to require Require JS (sounds like a tongue twister), even if this works it seems like more of a workaround than a fix :(
    – Ben Crook
    May 4, 2017 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.