如何提取html表中的文本


How to extract text in html table

我实现了一个代码,通过按下按钮可以生成html表:

function crea_tabella($colonne,$suddivisione,$righe)
{
    $dipendenti = 0;
    $progressivo = 0;
    echo "<table border='1' width='50%' height='25%'>";
        echo "<tr>";
            echo "<td></td>";
            echo "<td>Lunedì</td>";
            echo "<td>Martedì</td>";
            echo "<td>Mercoledì</td>";
            echo "<td>Giovedì</td>";
            echo "<td>Venerdì</td>";
            echo "<td>Sabato</td>";
        echo "</tr>";
        for($i=0; $i<$righe; $i++)
        {
            echo "<tr>";
            echo "<td ><input name='casella $progressivo' type='text' value='Operaio $i' disabled='true'></td>";
            $progressivo++;
            for($j=0; $j<$colonne;$j++)
            {
                if($suddivisione == 1)
                {   
                    echo "<td><input type='text name='dipendente''></td>";
                }
                elseif($suddivisione == 2)
                {    
                    echo "<td>AM<input type='text' name='dipendente'><br>";
                    echo "       PM<input type='text name='dipdenente''></td>";
                }
            }
            echo "</tr>";
        }
        echo "</table><br>";
}

现在我想创建另一个函数来提取生成的表的特定单元格的内容。。请参阅红框:
图像


怎么能做到这一点?

示例

操作1:a b c d e f
a b c d e f是Operaio 1 行的内容

您必须使用表单或javascript/jquery来完成此操作。下面是一个使用jquery的例子。我使用的主要功能是.click(),用于在单击按钮时触发值的收集,.each(),用于循环指定的输入,classes,用于指定我想要的输入。.append()用于输出结果

$(document).ready(function() {
    $('.get').on('click', function(e) {
        var values = [];
        e.preventDefault();
        $('.getValue').each(function() {
            values.push($(this).val());
        });
        $('.results').html('');
        for(var i=0; i<values.length; i++) {
            $('.results').append('<br/>' + values[i]);
        }
    });
});
td, th {
    border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <thead>
        <tr>
            <th></th>
            <th>Header 1</th>
            <th>Header 2</th>
            <th>Header 3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Left Column1</td>
            <td><input class="getValue" type="text" value="1,1"/></td>
            <td><input class="getValue" type="text" value="1,2"/></td>
            <td><input class="getValue" type="text" value="1,3"/></td>
        </tr>
        <tr>
            <td>Left Column2</td>
            <td><input class="getValue" type="text" value="2,1"/></td>
            <td><input class="getValue" type="text" value="2,2"/></td>
            <td><input class="getValue" type="text" value="2,3"/></td>
        </tr>
        <tr>
            <td>Left Column3</td>
            <td><input class="getValue" type="text" value="3,1"/></td>
            <td><input class="getValue" type="text" value="3,2"/></td>
            <td><input class="getValue" type="text" value="3,3"/></td>
        </tr>
    </tbody>
</table>
<br/>
<br/>
<a href="#" class="get">Get Values of Inputs</a><br/>
<div class="results"></div>

编辑:在这里显示代码更容易。关于你的粉盒:

if($suddivisione == 1)
{      
    echo "<td><input type='text name='dipendente''></td>";
}
elseif($suddivisione == 2)
{        
    echo "<td>AM<input type='text' name='dipendente'><br>";
    echo "PM<input type='text name='dipdenente''></td>";
}

不正确,必须将dipendente用作class,而不是name。而且你还有一堆打字错误。。。你的'报价不匹配。。。。

像这样的东西:

if($suddivisione == 1)
{      
    echo "<td><input type='text class='dipendente'></td>";
}
elseif($suddivisione == 2)
{        
    echo "<td>AM<input type='text' class='dipendente'><br>";
    echo "PM<input type='text' class='dipendente'></td>";
}

如果在你的代码中,其他任何东西都不起作用,我敢打赌,这只是你代码中的拼写错误(拼写错误,没有正确对齐引号)。