【问题标题】:Uncaught ReferenceError: is not defined onclick when using character strings未捕获的 ReferenceError:使用字符串时未定义 onclick
【发布时间】:2015-06-16 20:32:08
【问题描述】:

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

生成的链接:

<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”控制台错误。当像下面的示例一样传递一个数字时,一切正常,我没有收到任何错误,应用程序继续按预期工作。

例如(这有效):

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

完整的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();
}
}

【问题讨论】:

    标签: javascript jquery ajax onclick


    【解决方案1】:

    您需要将输入的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>';
    

    理想情况下,您应该在给按钮一个类之后附加一个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);
    });
    

    【讨论】:

    • 这非常有效。我记得在一些地方读到引文是可选的。我想在这种情况下它有点不同。谢谢!
    【解决方案2】:

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

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

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

    所以,当我使用 php,并且文档是 html 时,我想到了变量的撇号字符,在我使用 chrome 观察元素之后,首先将函数 alert 移动到父元素和子元素,即没解决。

    之后,同样在 specting 元素中,在 chrome F12 中,我尝试更改函数,包括 ''(我在 php 代码中传递的)到警报函数中的变量:onclick="alert(variable);"到 onclick="alert('variable');"我的警报起了作用。

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

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

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

    它会起作用的! (没有 Vue),好吗? :)

    【讨论】:

      猜你喜欢
      • 2013-06-27
      • 2020-11-20
      • 2023-01-23
      • 2016-11-03
      • 2011-01-05
      • 2016-01-02
      • 2013-10-06
      • 2016-12-17
      相关资源
      最近更新 更多