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
 asked May 4, 2017 at 7:56
 
 
 
 Hunter 
 
 1,4843 gold badges22 silver badges38 bronze badges
 
 1 Answer 1
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();
 }
 });
 });
});
 answered May 4, 2017 at 9:29
 
 
 
 Hammad Khalid 
 
 5625 silver badges18 bronze badges
 
 - 
 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– Ben Crook2017年05月04日 09:51:29 +00:00Commented May 4, 2017 at 9:51
Explore related questions
See similar questions with these tags.
default
 
 
 
VM4522:2 Uncaught ReferenceError: require is not defined? And did you clear the static content, redeploy it after your changes were made?require(['jquery', 'jquery/ui'], function($){, ive dont static content deploy numerous timebin/magento setup:upgrade(to remove static, generation, someother temporary files), thenbin/magento setup:static-content:deployand see if this error still happens.