简体   繁体   English

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

[英]Uncaught ReferenceError: is not defined onclick when using character strings

I am trying to pass a variable to the onClick function using a previously stored value.我正在尝试使用以前存储的值将变量传递给 onClick 函数。 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.例如,以下链接是在用户搜索邮政编码后使用 ajax 调用生成的。 The returned value "WAFHOH3" is the ID that is associated with that particular store:返回值“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.我的问题是,如果返回数字以外的任何内容,我会收到“未捕获的 ReferenceError:未定义 WAFHOH3”控制台错误。 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.当像下面的示例一样传递一个数字时,一切正常,我没有收到任何错误,应用程序继续按预期工作。

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

You need to wrap the input item.store_code with quotation marks;您需要用引号将输入的item.store_code包裹起来; 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>';

Ideally, you would attach a click handler after giving the buttons a class (such as register ):理想情况下,您应该在给按钮一个类(例如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);
});

I may be late, and maybe its an absolute mistake of me, but, i have to add my answer here because i just solved exactly the same situation in about three minutes ago .我可能迟到了,也许这是我的绝对错误,但是,我必须在这里添加我的答案,因为我刚刚在大约三分钟前解决了完全相同的情况。

I just solved this using the most simple sollution, and the error "Uncaught ReferenceError" from the console is solved, also i have my alert();我刚刚使用最简单的解决方案解决了这个问题,控制台中的错误“Uncaught ReferenceError”得到了解决,我也有我的 alert(); passing the variable as i needed.根据需要传递变量。

I also need to include that i did not aproove the sollution gave, about "not using" the alert function, once i searched for the sollution, not for another method for that .我还需要包括我没有同意给出的解决方案,关于“不使用”警报功能,一旦我搜索了解决方案,而不是另一种方法。

So, as i am using php, and the document is html, i thinked about the apostrophe charactere to the variable, after i had been spectating the element using chrome, first moving the function alert to the parent and child elements, that not solved .所以,当我使用 php 并且文档是 html 时,我想到了变量的撇号字符,在我使用 chrome 观察元素之后,首先将函数 alert 移动到父元素和子元素,这没有解决。

After, also in the specting element, inside chrome F12 i tryed changing the function, including '' (that i passed in php code) into variable inside the alert function as: onclick="alert(variable);"之后,同样在 specting 元素中,在 chrome F12 内部,我尝试更改函数,包括 ''(我在 php 代码中传递的)到警报函数内部的变量中:onclick="alert(variable);" to onclick="alert('variable');"到 onclick="alert('variable');" and my alert had worked .我的警报起了作用。

Ok.行。 So, i try everything to insert '' 2 single quotes '' to my variable in php, that seems impossible, even if i change all my code to " and use ' or the oposite .所以,我尝试一切在 php 中将 '' 2 个单引号 '' 插入到我的变量中,这似乎是不可能的,即使我将所有代码更改为 " 并使用 ' 或相反的 .

Then, i decided to try the most obvious and old school method, that is about charactere representation, and i cfound that ' (single quote) is represented by ' in php.然后,我决定尝试最明显和最古老的方法,即关于字符表示的方法,我发现 ' (单引号)在 php 中由 ' 表示。 Everything inside ->> ' <<-里面的一切 ->> ' <<-

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

It will work!它会起作用的! (with no Vue), ok ? (没有 Vue),好吗? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM