【问题标题】:convert html table data to form using jQuery使用 jQuery 将 html 表格数据转换为表单
【发布时间】:2011-05-09 13:31:18
【问题描述】:

我正在尝试使用 YQL 从另一个网站上的表中检索数据,并使用 jQuery 在我的网站上以表单形式显示数据。我已成功检索数据并将其按原样显示在我的网站上,但是我不确定从哪里开始将数据放入表单输入中。

正在检索的数据如下所示:

<table>
    <tbody>
        <tr class="">
            <td class="nobr">
                <a href="/player-26164302" title="View player profile">C Borup</a>
            </td>
            <td class="num IP">
                <p>0.2</p>
            </td>
            <td class="num H:pitching">
                <p>2</p>
            </td>
            [...]
        </tr>
    </tbody>
</table>

我想我现在可以做两件事中的一件。
1) 尝试将此表中的 p 标签转换为输入,但同样重要的是,我还需要将 td 中的类应用于输入。 2)从表中的p标签中取出值,并根据td类放在已有的表格中(这可能吗?)。

我的 jQuery 检索和显示数据如下所示:

jQuery("#bbnuke_retrieve_gamechanger_results_btn_id").bind('click', function() {
    var gameID = jQuery("#bbnuke_plugin_gamechanger_import").val();
    var pitchLink = 'http://www.gamechanger.io/game-' + gameID + '/boxscore/pitching/standard';
    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select tbody FROM html where url ="' + pitchLink + '" and xpath="//table[@id=' + "'tbl_home_pitching'" + ']"') + '&format=xml&callback=?';
    jQuery.getJSON(yql, cbFunc);

    function cbFunc(data) {
        if (data.results[0]) {
            data = data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
            jQuery("#gamesitedump").html(data);
        }
        else {
            jQuery("#gamesitedump").html("Error");
            throw new Error('Nothing returned from getJSON.');
        }
    }
});

非常感谢任何帮助!

【问题讨论】:

    标签: php jquery yql


    【解决方案1】:

    您可以通过这种方式将&lt;p&gt;“转换”为&lt;input&gt;

    $("td > p").each(function() {
        // creates a new <input> element
        $newInput = $("<input type='text'/>");
        // find the parent of the current <p>, and applies its class to the new <input>
        $newInput.addClass($(this).parent().attr("class"));
        // get the value of the current <p>, and set the value of the new <input> to this
        $newInput.val($(this).text());
        // replace the current <p> element with its <input> equivalent
        $(this).replaceWith($newInput);
    });
    

    您可以找到jsFiddle example here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      相关资源
      最近更新 更多