微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何运行onclick函数? | Java脚本

如何解决如何运行onclick函数? | Java脚本

尝试编写一个脚本,根据搜索栏查找表中的行并返回结果,但运行 onclick 函数时出现问题。我做错了什么?

 function tabletoArray(myTable) {
  var result = []
  var rows = myTable.rows;
  var cells,t;

  // Iterate over rows
  for (var i=0,iLen=rows.length; i<iLen; i++) {
    cells = rows[i].cells;
    t = [];

    // Iterate over cells
    for (var j=0,jLen=cells.length; j<jLen; j++) {
      t.push(cells[j].textContent);
    }
    result.push(t);
  }
  
  var programs = result.map(function(value,index) { return value[0]; }); 
  var search = document.getElementById('programInput').value.toupperCase();
  var wynik = result.filter(function(item){return item[0] === search; });
   
 return wynik;
}
    
    document.write(JSON.stringify(tabletoArray(document.getElementsByTagName('table')[0])));
 
<input type="text" placeholder="Search..." id="programInput">
 <button id="programbutton" type="submit" onclick="tabletoArray(myTable)">Search</button>
<div id="results">

</div>
<table id="myTable"  style="border: none;"><thead class="wp2s2fg_fetcher_table_thead" style="border:none; : 1px solid #ccc; "><tr class="wp2s2fg_fetcher_table_tr"><th class="wp2s2fg_fetcher_table_th " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:#fff; background-color:#fff; ">Program</th><th class="wp2s2fg_fetcher_table_th " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:#fff; background-color:#fff; ">Typ</th><th class="wp2s2fg_fetcher_table_th " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:#fff; background-color:#fff; ">Obsługiwany</th><th class="wp2s2fg_fetcher_table_th " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:#fff; background-color:#fff; ">Type</th></tr></thead><tbody class="wp2s2fg_fetcher_table_tbody"><tr class="wp2s2fg_fetcher_table_tr"><td class="wp2s2fg_fetcher_table_td " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:left; background-color:#fff; ">Program1</td><td class="wp2s2fg_fetcher_table_td " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:left; background-color:#fff; ">PP</td><td class="wp2s2fg_fetcher_table_td " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:left; background-color:#fff; ">TRUE</td><td class="wp2s2fg_fetcher_table_td " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:left; background-color:#fff; ">A</td></tr><tr class="wp2s2fg_fetcher_table_tr"><td class="wp2s2fg_fetcher_table_td " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:left; background-color:#fff; ">Program2</td><td class="wp2s2fg_fetcher_table_td " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:left; background-color:#fff; ">PP</td><td class="wp2s2fg_fetcher_table_td " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:left; background-color:#fff; ">FALSE</td><td class="wp2s2fg_fetcher_table_td " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:left; background-color:#fff; ">B</td></tr></tbody><tfoot class="wp2s2fg_fetcher_table_tfoot"><tr class="wp2s2fg_fetcher_table_tr"><td class="wp2s2fg_fetcher_table_td " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:left; background-color:#fff; ">Program3</td><td class="wp2s2fg_fetcher_table_td " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:left; background-color:#fff; ">PP</td><td class="wp2s2fg_fetcher_table_td " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:left; background-color:#fff; ">TRUE</td><td class="wp2s2fg_fetcher_table_td " style="font-size:16px; color:inherit; line-height:1; letter-spacing:1px; font-weight:normal; border:none; border: 1px solid #ccc; text-align:left; background-color:#fff; ">C</td></tr></tfoot></table>

解决方法

您无法访问像 myTable.rows 这样的行,因为 myTable 不是 json 对象,而是对 table html 元素的引用。您应该使用 document.queryselectorall 来获取表中的行和列。

,

我不认为你可以传递这样的变量。我会在 js 脚本中使用 switch (UrlRead(3)) { case 'verlogin': $email = $_POST['email']; $pass = $_POST['password']; if ($email == "deneme@deneme.com") { $Return['STATUS'] = "ERR_USERNAME_TAKEN"; $Return['ERR'] = array( 'email' => 'Username is Taken.' ); } else { $Return['STATUS'] = "SUCC"; $Return['ERR'] = array( 'email' => 'Username is available.' ); } break; } echo json_encode($Return,JSON_UNESCAPED_UNICODE); 作为按钮和表格。然后给按钮添加一个事件监听器。

我可能是错的,但我认为那是你的问题。希望这是有道理的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。