javascript 未捕获的 ReferenceError:在使用字符串时未定义 onclick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30877617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:52:15  来源:igfitidea点击:

Uncaught ReferenceError: is not defined onclick when using character strings

javascriptjqueryajaxonclick

提问by Infini7y

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:

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

Generated Link:

生成链接:

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

Based on this code:

基于此代码:

<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>

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.

我的问题是,如果返回数字以外的任何内容,我会收到“未捕获的引用错误:未定义 WAFHOH3”控制台错误。当一个数字像下面的例子一样被传递时,一切正常,我没有收到错误,应用程序继续按预期工作。

For example (This Works):

例如(本作品):

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.

我试过手动将字符串更改为数字只是为了隔离任何与数据库相关的问题。我唯一的猜测是我的代码中有一些东西可能试图将输入验证为数字。

The full code is below for the ajax call.

ajax 调用的完整代码如下。

Full Code:

完整代码:

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:[email protected]">[email protected]</a> for assistance.';
      }
      else {
        html += '<br/><br/><p>This is an example message, email us at <a href="mailto:[email protected]">[email protected]</a> for assistance.';
      }

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

Any guidance is appreciated.

任何指导表示赞赏。

回答by kevinji

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

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

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>';

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

理想情况下,您应该click在给按钮一个类(例如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);
});