未捕获的ReferenceError:使用字符串时未在onclick上定义 [英] Uncaught ReferenceError: is not defined onclick when using character strings

查看:103
本文介绍了未捕获的ReferenceError:使用字符串时未在onclick上定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以前存储的值将变量传递给onClick函数.我有一个数据库设置,当提供邮政编码时,它会搜索商店位置.例如,在用户搜索邮政编码后,使用ajax调用生成以下链接.返回值"WAFHOH3"是与该特定商店关联的ID:

I am trying to pass a variable to the onClick function using a previously stored value. I have a database setup that searches for store locations when provided with a ZIP code. For example, the following link is generated using an ajax call after a user searches for a Zip Code. The returned value "WAFHOH3" is the ID that is associated with that particular store:

生成的链接:

<input type="button" onclick="myfunction(WAFHOH1);" value="This Is My Store" data-store-code="WAFHOH3">

基于此代码:

<div class="col-sm-3"><input type="button" onclick="myfunction(' + item.store_code + ');" value="This Is My Store" data-store-code="' + item.store_code + '"></div>

我的问题是,如果返回数字以外的任何内容,我会收到未捕获的ReferenceError:未定义WAFHOH3"控制台错误.当像下面的示例那样传递数字时,一切正常,并且我没有出现任何错误,并且该应用程序继续按预期运行.

My problem is that if anything other than a number is returned I get a "Uncaught ReferenceError: WAFHOH3 is not defined" console error. When a number is passed like the example below, everything works fine and I get no errors and the application continues to work as expected.

例如(此方法有效):

我尝试过手动将字符串更改为数字,只是为了隔离任何与数据库相关的问题.我唯一的猜测是我的代码中可能正在尝试验证输入为数字.

Ive tried manually changing the character string to numbers only to isolate any database related issues. My only guess is that there is something in my code that is maybe attempting to verify the input as number.

下面是ajax调用的完整代码.

The full code is below for the ajax call.

完整代码:

function myFunction() {
var searchValue = $('#foobar').val();

if (searchValue.length > 3) {
  var acs_action = 'searchCction';

  $.ajax({
    async: false,
    url: mysearchurl.url+'?action='+acs_action+'&term=' + searchValue,
    type: 'POST',
    data: {
      name: searchValue
    },
    success: function (results) {
      var data = $.parseJSON(results); 

      $('#resContainer').hide();

      var html = '';

      if (data.length > 0) {
        html += '<br/><br/><ul>';
        for (var i = 0; i < data.length; i++) {
          var item = data[i];
          html += '<li>';
          html += '<div class="row myclass">';
          html += '<div class="col-sm-9">';
          html += ' <h3>' + item.label + '</h3>' ;
          html += ' <span>' + item.desc + '</span>';
          html += '</div>'
          html += ' <div class="col-sm-3"><input type="button" onclick="dofunction(' + item.store_code + ');" value="This Is My Store" data-store-code="' + item.store_code + '"></div>';

          html += '</div>';
          html += '</li>';
        }
        html += '</ul><br/><br/><p>This is an example message please email us at <a href="mailto:admin@admin.com">admin@admin.com</a> for assistance.';
      }
      else {
        html += '<br/><br/><p>This is an example message, email us at <a href="mailto:sadmin@admin.com">admin@admin.com</a> for assistance.';
      }

      $('#foo').html(html);
      $('#foo').show();
      $('.foobar').hide();
    }
  });
} else {
  $('#foo').hide();
}
}

任何指导表示赞赏.

推荐答案

您需要用引号将输入item.store_code括起来;否则,它将尝试将其视为变量,而不是字符串:

You need to wrap the input item.store_code with quotation marks; otherwise, it tries to treat it as a variable, not a string:

html += '<div class="col-sm-3"><input type="button" onclick="noActivationCodeRegistration(\'' + item.store_code + '\');" value="This Is My Store" data-store-code="' + item.store_code + '"></div>';

理想情况下,在为按钮提供类(例如register)之后,您应该附加click处理程序:

Ideally, you would attach a click handler after giving the buttons a class (such as register):

html += '<div class="col-sm-3"><input type="button" class="register" value="This Is My Store" data-store-code="' + item.store_code + '"></div>';

// Later
$('.register').on('click', function() {
    var storeCode = $(this).data('storeCode');
    noActivationCodeRegistration(storeCode);
});

这篇关于未捕获的ReferenceError:使用字符串时未在onclick上定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆