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

时间:2015-01-06 07:35:29

标签: javascript jquery onclick

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

$(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);
    });

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

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

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

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

2 个答案:

答案 0 :(得分:2)

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

同样在您的代码中,您为单个tbody创建多个table,在循环之前使用以下2行,

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

&#13;
&#13;
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);
    });
});
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试替换这些行:

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

使用此代码:

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