【问题标题】:Two functions don't work together, but no errors两个函数不能一起工作,但没有错误
【发布时间】:2013-07-11 16:33:51
【问题描述】:

我有几个 javascript 函数:

  • 显示/隐藏一些表格行
  • 添加新行

两者都在同一个页面上工作,但不是在特定情况下。

这是下面代码的fiddle

/*************** show/hide sections ***************/
$('.arrow').click(function(event) {
    var sec_id = $(this).attr('id').split('.')[0];  // admin.link -> admin

    if ($(this).closest('td').attr('class') == 'subtitle')
        $('.s_'+sec_id).toggle();                   // toggle hide/show for 1 item (section)
    else
        $(this).closest('tr').nextUntil('tr:not(.s_' + sec_id+')').toggle();

});

/*************** Add rows ***************/
$('.add_row').click(function(event) {
    var sid = $(this).attr('sid');                  // the id of the <tbody>
    var tbody = document.getElementById(sid);   // the <tbody> to add rows to

    // === GENERATE NEW NAMES for inputs
    // get the name of the first input in the last row
    var rows = tbody.rows;
    var rowinput = rows[rows.length-1].getElementsByTagName('INPUT');
    // split name into array
    var name_piece = rowinput[0].name.split('][');
    // create name for next row; check if last row is a blank row
    var reg = new RegExp('^[0-9]+$');
    if (reg.test(name_piece[1]))    // if integer
        var iteration = parseInt(name_piece[1], 10) + 1;    // convert to int with base 10
    else
        iteration = 1;
    if (iteration < 10)
        iteration = '0'+iteration;                  // add left padding to ensure sort order
    var front = 'items['+sid.substring(2)+']['+iteration+']';   // front of input name (remove the 's_' from name)
    // END GENERATE NEW NAMES for inputs

    // === CREATE ROW
    var row = document.createElement('tr');         // create a row
    var td1 = document.createElement('td');         // create first cell
    td1.setAttribute('colSpan', 2);
    td1.innerHTML = '<input type="text" name="'+front+'[desc]" maxlength="100" />';
    var td2 = document.createElement('td');         // create second cell
    td2.innerHTML = '<input type="text" name="'+front+'[price]" maxlength="9" onChange="calc_ttl()" class="right small" />';
    var td3 = document.createElement('td');         // create third cell
    td3.setAttribute('colSpan', 3);
    // END CREATE ROW

    // output
    row.appendChild(td1);
    row.appendChild(td2);
    row.appendChild(td3);
    tbody.appendChild(row);
});

在小提琴中,您会看到 3 个链接,它们都可以正常工作。唯一的例外是如果我在显示隐藏行的同时添加一行;例如:

  1. 点击“子部分”显示行
  2. 点击“添加行”
  3. 单击“子部分”以隐藏行此处失败

从那时起,除非我重新加载页面,否则“子部分”链接将不再有效。代码签出,Firebug 没有报告错误,所以我很茫然。非常感谢任何建议。

【问题讨论】:

  • 这是一个非常大的代码转储。我建议将其减少到最必要的部分,以获得更多更好的答案
  • 你们为什么不将 Jquery 添加到您的问题标签中

标签: javascript jquery


【解决方案1】:

问题不在于您在此处发布的 jQuery 代码。它在 HTML 中。在你的小提琴中,你有:

<tr onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'" class="s_grp_2d_ttl_asset" style="display: none;">

一旦该行可见,只要鼠标移到它上面,s_grp_2d_ttl_asset 类就会被 highlight 类替换,这会使您的点击事件停止在第一个元素处。如果您改用 addClass、removeClass 或 toggleClass 函数,则可以在不完全删除原始类的情况下进行更改。

【讨论】:

  • 非常感谢,这让我发疯了!虽然我想知道为什么当我将鼠标悬停在任何隐藏的行上时这个问题没有出现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-26
  • 2020-06-30
  • 1970-01-01
  • 2020-12-13
相关资源
最近更新 更多