如何在动态行点击事件中传递参数

时间:2022-10-23 20:38:17

I have a search button on body. On this search button click i am calling the button click event as:

我身上有一个搜索按钮。在此搜索按钮上单击我将按钮单击事件调用为:

$(document).ready(function() {

   //On Search Button click, this function creates dynamic tables.
   $("#searchBtn").click(function() {
        var numArray = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
        var alphaArray = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");

        var table = $('<table></table>').addClass('table');

        for (i = 0; i < numArray.length; i++) {
          var tbody = $('<tbody></tbody>');
          var row = $('<tr></tr>');
          var num = numArray[i];
          var alpha = alphaArray[i];
          row.on("click", function() {
            selectRow(num, alpha);
          });

          var cell1 = $('<td></td>').addClass('spacer').text(numArray[i]);
          var cell2 = $('<td></td>');
          var button;
          if (alpha[i] == "b") {
            button = $('<button type="button">').addClass('btn btn-primary').text('BOY');
          } else if (alpha[i] == "g") {
            button = $('<button type="button">').addClass('btn btn-success').text('GIRL');
          } else if (alpha[i] == "h") {
            button = $('<button type="button">').addClass('btn btn-warning').text('HI');
          }

          cell2.append(button);
          row.append(cell1);
          row.append(cell2);
          tbody.append(row);
          table.append(tbody);
        }

        $('#searchTable').append(table);
    });

Now on individual row click i am calling a function selectRow(num,alpha) which expects two paramters as:

现在在单独的行上单击我调用一个函数selectRow(num,alpha),它需要两个参数:

function selectRow(num,alpha){
            $(document).ready(function() {              
                    alert("Number and Alphabet is  "+num + alpha );
});

But when the control comes to the above function the alert always prints the last record of both the array.

但是当控件进入上述函数时,警报始终会打印出两个数组的最后一条记录。

What am i doing wrong? Looking forward to your answers.

我究竟做错了什么?期待您的回答。

2 个解决方案

#1


2  

Try to pass this in your function and use data-* attribute.

尝试在您的函数中传递此函数并使用data- *属性。

Also in your code you are creating multiple tbody for a single table, use the below 2 lines before your loop like,

同样在你的代码中你要为一个表创建多个tbody,在你的循环之前使用下面的2行,

var tbody = $('<tbody></tbody>');
tbody.append(row);

function selectRow(ths){
   alert("Number and Alphabet is  "+$(ths).data('num') + $(ths).data('alpha'));
};
$(function () {
    $("#searchBtn").click(function () {
        var numArray = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
        var alphaArray = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");

        var table = $('<table></table>').addClass('table');
        var tbody = $('<tbody></tbody>');
        table.append(tbody);
        for (i = 0; i < numArray.length; i++) {
            var num = numArray[i];
            var alpha = alphaArray[i];
            var row = $('<tr data-num="'+num+'" data-alpha="'+alpha+'"></tr>');
            row.on("click", function () {
                selectRow(this);
            });

            var cell1 = $('<td></td>').addClass('spacer').text(numArray[i]);
            var cell2 = $('<td></td>');
            var button;
            if (alpha == "b") {
                button = $('<button type="button">').addClass('btn btn-primary').text('BOY');
            } else if (alpha == "g") {
                button = $('<button type="button">').addClass('btn btn-success').text('GIRL');
            } else if (alpha == "h") {
                button = $('<button type="button">').addClass('btn btn-warning').text('HI');
            }
            cell2.append(button);
            row.append(cell1);
            row.append(cell2);
            tbody.append(row);
        }
        $('#searchTable').append(table);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="searchBtn">Search</button>
<div id="searchTable"></div>

#2


0  

Try replacing these lines :

尝试替换这些行:

var row = $('<tr></tr>');
var num =numArray[i];
var alpha =alphaArray[i];
row.on("click", function () {
  selectRow(num, alpha);
});

with this code:

使用此代码:

var num =numArray[i];
var alpha =alphaArray[i];
var row = $('<tr onclick="selectRow(' + num + ',' + alpha + ');"></tr>');

#1


2  

Try to pass this in your function and use data-* attribute.

尝试在您的函数中传递此函数并使用data- *属性。

Also in your code you are creating multiple tbody for a single table, use the below 2 lines before your loop like,

同样在你的代码中你要为一个表创建多个tbody,在你的循环之前使用下面的2行,

var tbody = $('<tbody></tbody>');
tbody.append(row);

function selectRow(ths){
   alert("Number and Alphabet is  "+$(ths).data('num') + $(ths).data('alpha'));
};
$(function () {
    $("#searchBtn").click(function () {
        var numArray = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
        var alphaArray = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");

        var table = $('<table></table>').addClass('table');
        var tbody = $('<tbody></tbody>');
        table.append(tbody);
        for (i = 0; i < numArray.length; i++) {
            var num = numArray[i];
            var alpha = alphaArray[i];
            var row = $('<tr data-num="'+num+'" data-alpha="'+alpha+'"></tr>');
            row.on("click", function () {
                selectRow(this);
            });

            var cell1 = $('<td></td>').addClass('spacer').text(numArray[i]);
            var cell2 = $('<td></td>');
            var button;
            if (alpha == "b") {
                button = $('<button type="button">').addClass('btn btn-primary').text('BOY');
            } else if (alpha == "g") {
                button = $('<button type="button">').addClass('btn btn-success').text('GIRL');
            } else if (alpha == "h") {
                button = $('<button type="button">').addClass('btn btn-warning').text('HI');
            }
            cell2.append(button);
            row.append(cell1);
            row.append(cell2);
            tbody.append(row);
        }
        $('#searchTable').append(table);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="searchBtn">Search</button>
<div id="searchTable"></div>

#2


0  

Try replacing these lines :

尝试替换这些行:

var row = $('<tr></tr>');
var num =numArray[i];
var alpha =alphaArray[i];
row.on("click", function () {
  selectRow(num, alpha);
});

with this code:

使用此代码:

var num =numArray[i];
var alpha =alphaArray[i];
var row = $('<tr onclick="selectRow(' + num + ',' + alpha + ');"></tr>');