未捕获的ReferenceError:< firebase字符串>未在HTMLTableRowElement.onclick上定义 [英] Uncaught ReferenceError: <firebase string> is not defined at HTMLTableRowElement.onclick

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

问题描述

伙计们,我创建了一个简单的动态表,该表从Firebase读取数据.这是表格的代码:

Hey guys I created simple, dynamic table that reads data from Firebase. Here is the code for table:

userRef.on("child_added", snap => {
                 var username = snap.child("name").child("name").val();
                 var user_surname = snap.child("name").child("surname").val();
$("#user_table").append("<tr onClick=\"displayCos(" + user_surname + ")\"><td>"+ username +"</td></tr>")

如您所见,该表显示用户名,当我单击它时,我想在alert中显示用户姓,这是displayCos功能代码:

as you can see, the table displays usernames and when i click on it i want to display user surname in alert, here's displayCos function code:

function displayCos(surname){
  if(surname != null){
  $("#surname_id").text(surname)
  }else {
  $("#surname_id").text("null");
 }

这是我的错误,当我单击用户名时,调试器显示此错误:Uncaught ReferenceError: (Here is user surname, let say: JOHNSON) is not defined at HTMLTableRowElement.onclick.我还发现,当我在Firebase中将包含字母和数字的内容更改为username_surname时,错误如下所示:Uncaught SyntaxError: Invalid or unexpected token,并且当该值仅为数字时,一切正常. 我可能尝试了StackOverflow的所有解决方案,但都不是我的问题的解决方案.我认为我的值不是一个字符串,可悲的是,它不是jQuery和Javascript的新手,只能靠我自己弄不明白,所以我恳求帮助:)

and here comes my error, when i'm clicking on username, debugger shows this error: Uncaught ReferenceError: (Here is user surname, let say: JOHNSON) is not defined at HTMLTableRowElement.onclick. I also find out that when i change username_surname in firebase for something that contains letters and numbers the error looks like that: Uncaught SyntaxError: Invalid or unexpected token, and when the value is only numeric everything works fine. I tried probably all solutions from StackOverflow and all of them aren't solution for my problem. I think that my value is not a string, as it should, sadly im new to jQuery and Javascript and can't figure it out only by myself, so I'm begging for help :)

@edit html代码,没有firebase详细信息,如果它们很重要,我将对此进行更新:

@edit html code, without firebase details, if they're important i will update this:

<html lang="en">
 <head>

  <title>Wyplata</title>
  <link rel="stylesheet" href="style.css">

 </head>

 <body>

   <div id="login_div" class = "login-page">
     <div class = "form">
       <div class = "register-form">

         <input type = "text" placeholder="Imie" id="email_field"/>
         <input type = "text" placeholder="Hasło" id="password_field"/>
         <button onclick="login()" > Zaloguj </button>

       </div>
    </div>
  </div>

  <div id="company_div" class = "company-page">
    <div id="user_table" class = "userui">
      <button onclick="logout()" > Wyloguj </button>
    </div>
  </div>

推荐答案

$("#user_table").append("<tr onClick=\"displayCos('" + user_surname + "')\"><td>"+ username +"</td></tr>")

请参见上文.您应该用引号将user_name括起来,以获取displayCos('Johnson').

See above. You should surround the user_name with quotes, in order to get displayCos('Johnson').

如果得到displayCos(Johnson),则表示Johnson是变量名,JavaScript尝试将该变量的值作为displayCos函数的参数传递.

If you get displayCos(Johnson) it means that Johnson is a variable name, and JavaScript tries to pass the value of this variable as argument of the displayCos function.

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

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