在php中运行javascript代码;不起作用


Running javascript code inside php doesn't work

这是我的全部javascript代码我在这里要做的是循环浏览html表,查找选中的复选框,并检索每行选中复选框中的数据但我需要运行这是php。

<script type='text/javascript'>//<![CDATA[
    $(window).load(function () {
        $('#save').click(function () {
            $('#dataTable').find('tr').each(function () {
                var row = $(this);
                if (row.find('input[type="checkbox"]').is(':checked') ) {
                    //alert('You must fill the text area!');
                    var $row1 = $(this).closest("tr"),        // Finds the closest row <tr> 
                    $tdi = $row1.find("td:nth-child(1)");
                    $.each($tdi, function () {                // Visits every single <td> element
                        var thirdrowval = $(this).text();         // Prints out the text within the <td>
                        //document.getElementById("signatoryid").value = thirdrowval
                        alert(thirdrowval);
                    });
                }
            });
        });
    });//]]> 
</script>

在阅读了这个网站后,我找到了一种方法,这是代码。但它不运行javascript。我预计会弹出警报。但它并没有像预期的那样工作

$row1 = "";
$row = "";
$thirdrowval = "";
$tdi = "";
echo "
        <script type='"text/javascript'">
                    $('#dataTable').find('tr').each(function () {
            var row = $(this);
            if (row.find('input[type='checkbox']').is(':checked') ) {
                var $row1 = $(this).closest('tr'),       
                $tdi = $row1.find('td:nth-child(1)');
                $.each($tdi, function () {                
                    var thirdrowval = $(this).text();         
                    alert(thirdrowval);
                });
            }
        });
        </script>
    ";

尝试将表$('#dataTable')也包含在php文件中。我认为javascript找不到名为#dataTable 的元素

在行中

if (row.find('input[type='checkbox']').is(':checked') ) {

你将不得不逃脱内部报价'它应该是

if (row.find('input[type=''checkbox'']').is(':checked') ) {

你还必须注意像这样带有双引号的字符串中的变量

echo "$row1";

将替换为php变量$row1的值。因此,在您的示例中,它将被替换为一个空字符串。如果这不是预期的行为,你可以使用单引号:

echo '$row1';

这将打印$row1