Jquery查找文本并更改单个单元格值 [英] Jquery find text and change single cell value

查看:112
本文介绍了Jquery查找文本并更改单个单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图解决这个问题。我正在尝试使用JQuery来查找具有精确值的单元格,然后更改该精确单元格中的文本而不会删除表格的其余部分。

I've been trying to figure this out. I'm trying to use JQuery to find a cell with an exact value and then change the text in that exact cell without obliterating the rest of the table.

这是一个非常简单的HTML表格:

Here's a very simple HTML table:

<table border='1'>
    <tr>
        <td>
            <table border='1'>
                <tr>
                    <td>
                        Value 1
                    </td>
                    <td>
                        1234
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td>
            <table border='1'>
                <tr>
                    <td>
                        Value 1.1
                    </td>
                    <td>
                        5678
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>

我想找到与值1完全匹配的单元格,然后将其更改为价值最高不改变任何其他单元格(因此它不会意外地匹配单元格值1.1)

I'd like to find the cell that exactly matches "Value 1" and then change it to "Value TO THE MAX" without changing any other cells (so it can't accidently match the cell "Value 1.1")

我最近/未崩溃的尝试:

My most recent/non-crashing attempt:

$("td:contains('Value 1')").text("Value 1.TO-THE-MAX");

从我读过的,我的问题是该表与此搜索匹配,因为该表包含细胞。这是我的JSFiddle: http://jsfiddle.net/sonoflysander/9gBqU/15

from what I've read, my problem is that the table matches to this search because the table contains the cell. Here's my JSFiddle: http://jsfiddle.net/sonoflysander/9gBqU/15

奖励积分:之后我会尝试做什么,我想务实地接下来立即获取单元格(在这种情况下单元格值为1234)所以我也可以任意改变它的价值。

Bonus Points: What I'm going to try to do afterwards, I'd like to pragmatically get the cell immediately next (in this case cell with value "1234") so I can change it's value arbitrarily as well.

一如既往,我们非常感谢任何帮助。

As always, any help is much appreciated.

根据gustavohenke的回答,我已经抽象了一个函数,我会在这里为那些寻找一些东西的人提供这个函数比我的确切场景更通用。

Based on gustavohenke's answer, I've abstracted a function which I'll include here for people looking for something a little more generic than my exact scenario.

function findString(search, element) {
    search = (typeof search === 'RegExp') ? search : new RegExp('^\\s*' + String(search) + '\\s*$');
    element = (typeof element === 'undefined') ? '*' : element;
    var x = $(element).filter(function () {
        return search.test($(this).text());
    });
    return x;
}

方法签名:

findString(search [, element])

search 可以是字符串或正则表达式,元素是可选的。如果没有提供它将搜索整个身体。为了性能,我建议你指定元素

search can be either a string or a regex, element is optional. If not provided it'll search the WHOLE body. For the sake of performance I recommend you specify element.

更新JSFiddle: http://jsfiddle.net/sonoflysander/9gBqU/

Updated JSFiddle: http://jsfiddle.net/sonoflysander/9gBqU/

推荐答案

var affected = $("td").filter(function() {
  // You should use regex here because you'll likely to receive whitespaces in the .text() call
  return /^\s*Value 1\s*$/.test( $( this ).text() );
}).text( "Value 1.TO THE MAX" );

// Now you apply what you want to what comes after that <td>s
affected.next().text( "I'm next to an awesome <td> affected by my code!" );

这篇关于Jquery查找文本并更改单个单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆