简体   繁体   English

使单元格中的文本成为按钮 javascript

[英]Making text in a cell a button javascript

is there anyway to make this extra td a button so that every "show" is clickable?有没有办法让这个额外的 td 成为一个按钮,以便每个“节目”都可以点击?

$(document).ready(function(){
                $.getJSON("http://cs1.utm.edu/~bbradley/map1/customers1.json", function(data){
                  var custData = '';
                  $.each(data, function(key, value){
                    custData += '<tr>';
                    custData += '<td>' +value.name+'</td>';
                    custData += '<td>' +value.address+'</td>';
                    custData += '<td>' +value.cityStateZip+'</td>';
                    custData += '<td>' +value.latLang+'</td>';
                    custData += '<td>' +(value.image || "No image available")+'</td>';
                    custData += '<td>' +"show"+ '</td>'; // this is what I need a button
                    custData += '</tr>';
                  });

I'm worried that I messed up by adding that extra column alongside the others being created with json data, but I'm not sure how else to add a button for every row.我担心我会在使用 json 数据创建的其他列旁边添加额外的列而搞砸了,但我不确定如何为每一行添加一个按钮。

I'm not sure I understand your question.我不确定我是否理解你的问题。 But here is an example for you that prints row numbers when clicked 'show'.但这里有一个例子,当点击“显示”时打印行号。

function printRowNumber(row) {
alert('Row:' + row);
return false;
}

$(document).ready(function() {
const appDiv = document.getElementById('app');
$.getJSON("http://cs1.utm.edu/~bbradley/map1/customers1.json", function(data) {
    var custData = '';
    $.each(data, function(key, value) {
        custData += '<tr>';
        custData += '<td>' + value.name + '</td>';
        custData += '<td>' + value.address + '</td>';
        custData += '<td>' + value.cityStateZip + '</td>';
        custData += '<td>' + value.latLang + '</td>';
        custData += '<td>' + (value.image || "No image available") + '</td>';
        custData += '<td>' + '<a href="javascript:void" onClick=printRowNumber(' + key + ')>show</a>' + '</td>'; // this is what I need a button
        custData += '</tr>';

    });
    appDiv.insertAdjacentHTML('beforeend', '<table>' + custData + '</table>');


})});

If you want to use button instead of 'show' you can replace a tag with button.如果您想使用按钮而不是“显示”,您可以用按钮替换标签。

    custData += '<td>' +'<button onClick=printRowNumber('+key+')>show</button>'+ '</td>'; 

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

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