Uncaught ReferenceError: is not defined onclick when using character strings

Uncaught ReferenceError: is not defined onclick when using character strings

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

生成Link:

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

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

例如(这有效):

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

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

完整代码:

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用引号引起来;否则,它会尝试将其视为变量,而不是字符串:

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

理想情况下,您可以在为按钮提供 class(例如 register)后附加一个 click 处理程序:

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);
});

我可能迟到了,也许这绝对是我的错误,但是,我必须在这里添加我的答案,因为我在大约三分钟前刚刚解决了完全相同的情况。

我刚刚使用最简单的解决方案解决了这个问题,控制台的错误“Uncaught ReferenceError”也解决了,我也有我的 alert();根据需要传递变量。

我还需要说明一下,我没有批准解决方案,关于“不使用”警报功能,一旦我搜索了解决方案,而不是另一种方法。

因此,当我使用 php 并且文档是 html 时,我想到了变量的撇号字符,在我使用 chrome 观察元素之后,先把函数alert移到父子元素上,没解决.

之后,同样在检查元素中,在 chrome F12 中,我尝试更改函数,包括“”(我在 php 代码中传递的)到警报函数中的变量,如:onclick ="警报(变量);"到 onclick="alert('variable');"我的警报奏效了。

好的。所以,我尽一切努力在 php 中向我的变量插入 '' 2 个单引号 '',这似乎是不可能的,即使我将所有代码更改为 " 并使用 ' 或相反的 .

然后,我决定尝试最明显和老派的方法,即关于字符表示,我发现'(单引号)在php中由'表示。里面的所有内容 ->> ' <<-

我的 php 代码是这样的:onclick="alert(&#039'.$variable.'&#039);"

会成功的! (没有 Vue),好吗? :)