繁体   English   中英

如何从中获取NodeValue <td> 输入类型

[英]How to get the NodeValue from <td> type input

我正在使用此HTML,并且尝试解析它以获取表列中所有元素的nodeValue

<table id="custinfo">
    <tr>
        <td><label>First Name</label></td> 
        <td><input type="text" name="firstname" ></td>    
    </tr> 
    <tr>
        <td><label>Last Name</label></td>
        <td><input type="text" name="lastname" ></td>   
    </tr>
    <tr>
        <td><label>Phone Number</label></td> 
        <td><input type="text" name="email" ></td>
    </tr>   
</table>

这是PHP。它仅适用于标签,不适用于输入类型。

$Dom= new DOMDocument();   
libxml_use_internal_errors(true);
$Dom->loadHTMLFile('Pre_order.html'); 
$info=$Dom->getElementById('custinfo');
$inforows=$info->getElementsByTagName("tr");
$input_tags = $Dom->getElementsByTagName('input'); 
$fnamecol=$inforows->item(0)->getElementsByTagName("td");
$fname=$fnamecol->item(1)->nodeValue; //this is returning null Instead of returning the input text value.

请改用simple_dom_parser.php库(下载库: https : //sourceforge.net/projects/simplehtmldom/files/和docs: http ://simplehtmldom.sourceforge.net/)

require_once('simple_dom_parser.php');

// Create DOM from file
$html = file_get_html('Pre_order.html');


// Find all labels
foreach($html->find('label') as $label)
       echo $label->plaintext . '<br>';


// Find all inputs
foreach($html->find('input') as $input)
       echo $input->name . '<br>'; 

在将值实际添加到输入元素之后,请尝试以下操作

$input_tags->item(1)->getAttribute('value')

(顺便说一句,在示例中检查$Dom$dom的情况。)

这是在http://sandbox.onlinephpfunctions.com/执行时可以工作的一些示例代码:

<?php

$htmlString = <<<HTML
<table id="custinfo">
    <tr>
        <td><label>First Name</label></td> 
        <td><input type="text" name="firstname" value="Jane"></td>    
    </tr> 
    <tr>
        <td><label>Last Name</label></td>
        <td><input type="text" name="lastname" value="Doe"></td>   
    </tr>
    <tr>
        <td><label>Phone Number</label></td> 
        <td><input type="text" name="email" value="212-555-1212"></td>
    </tr>   
</table>
HTML;

$dom= new DOMDocument();
$dom->loadHTML($htmlString); 
$input_tags = $dom->getElementsByTagName('input'); 

echo $input_tags->item(1)->getAttribute('value'); // "Doe"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM