jQuery搜索多个表格以查找文本,如果找到文本则单击一个按钮

时间:2016-03-30 10:56:23

标签: javascript jquery imacros

我正在尝试搜索某些表格并单击特定表格的单元格内的按钮。这是我目前的代码

links[1].click(); 
iimPlayCode('WAIT SECONDS = 2')
var compTabs = window.content.document.getElementById('compTabs')
var compTabList = compTabs.getElementsByClassName("tabNavigation top_tabs")
if (compTabList[0].children[0].innerText.match(/voting/gi)) {
    $('#candidatesList').each(function () {
        if ($(this).text().match(/john/gi)) { // search for text
           // everything works fine until here, don't know how to click the specific button         
           //get id of button and click it
        }
    })
}

这是$(this).text():

的结果
John
100%
05/09/2013
Vote

这将是$(this).html()

的结果
<tr style="text-align: center;">
    <td><img src="http://somewebsite.com" width="36px"></td>
    <td>John</td>
    <td>100%</td>
    <td>05/09/2013</td>
    <td class="last-child"><button rel="123" id="6332" class="button_85 sa_vote">Vote</button></td>
</tr>

我要做的是点击具有该特定ID的按钮,在这种情况下为“6332”。

这是一个更完整的HTML代码。

<div id="compTabs">
                            <div class="tabs">
                                <ul class="tabNavigation top_tabs">
                                                                         <li rel="#tab_voting" class="selected">
                                            <table cellspacing="0" cellpadding="0">
                                                    <tbody><tr>
                                                            <td width="28px"><img src="http://somewebsite.com/design/img/new_style/tabs/voting.png"></td>
                                                            <td>Voting</td>
                                                    </tr>
                                            </tbody></table>
                                    </li>
                                                                        <li rel="#tab_shareholders">
                                            <table cellspacing="0" cellpadding="0">
                                                    <tbody><tr>
                                                            <td width="28px"><img src="http://somewebsite.com/design/img/new_style/tabs/shareholders.png"></td>
                                                            <td>Shareholders</td>
                                                    </tr>
                                            </tbody></table>
                                    </li>
                                </ul>

                                                                <div class="compTabsContent" id="tab_voting" style="display: block;">      
<div style="height:390px;margin:0;" class="companyBody">

        <table cellspacing="0" cellpadding="0" width="100%" style="margin-top: 10px;" class="proTable">
            <tbody><tr valign="top">
                <td style="text-align:center;height:35px;vertical-align:middle;font-size:14px;">
                    Vote to elect <b style="color:red"> General Director </b>.                    <div id="startTimer" class="timerStyle01">1d 22h:13m:30s</div>
                </td>
            </tr>
        </tbody></table>

        <table cellspacing="0" cellpadding="0" width="100%" style="margin-top:10px">
            <tbody><tr valign="top"><td width="35%">
                <table cellspacing="0" cellpadding="0" class="proTable newTable">
                    <thead>
                        <tr><th class="thb" colspan="3">All shareholders</th></tr>
                        <tr>
                            <th width="36px">&nbsp;</th>
                            <th>Shareholder name</th>
                            <th>He voted</th>
                    </tr></thead>
                    <tbody id="listOfAdministrators">

                                    <tr style="text-align: center;">
                                    <td><img width="36px" src="http://somewebsite.com/content/user/small/user_1364379268_234.jpg"></td>
                                    <td>John</td>
                                    <td><img src="http://somewebsite.com/design/img/new_style/tabs/no.png"></td>
                                    </tr>
                                                    </tbody>
                </table> 
            </td>
            <td width="10px"></td>
            <td>
                <table cellspacing="0" cellpadding="0" class="proTable newTable">
                            <thead>
                                <tr><th class="thb" colspan="5">Vote to elect <b style="color:red"> General Director </b>.</th></tr>
                                <tr>
                                    <th width="36px">&nbsp;</th>
                                    <th>Shareholder name</th>
                                    <th>Shares</th>
                                    <th>Date of shares purchase</th>
                                    <th>&nbsp;</th></tr>
                            </thead>
                            <tbody id="candidatesList">

                                                <tr style="text-align: center;">
                                                    <td><img width="36px" src="http://somewebsite.com/content/user/small/user_1364379268_234.jpg"></td>
                                                    <td>John</td>
                                                    <td>100%</td>
                                                    <td>05/09/2013</td>
                                                    <td class="last-child"><button class="button_85 sa_vote" id="60928" rel="234">Vote</button></td>
                                                </tr>
                                                                        </tbody>
                </table>
            </td></tr>
        </tbody></table>


</div></div>
                                                                <div class="compTabsContent" id="tab_shareholders" style="display: none;"><div style="height:390px;margin:0;" class="companyBody">
    <table cellspacing="0" cellpadding="0" style="margin-top:15px;font-size: 11px;" class="proTable newTable">
        <thead>
            <tr><th style="font-size:13px;" class="thb" colspan="4">All shareholders</th></tr>
            <tr>
                <th width="36px" style="border-left: 1px solid #d2d2d2;">&nbsp;</th>
                <th>Shareholder name</th>
                <th>Shares</th>
                <th>Date of shares purchase</th>
            </tr>
        </thead>
        <tbody>

                            <tr style="text-align: center;">
                                <td><img width="36px" src="http://somewebsite.com/content/user/small/user_1364379268_234.jpg"></td>
                                <td>John</td>
                                <td>100%</td>
                                <td class="last-child">05/09/2013</td>
                            </tr>
                                </tbody>
    </table> 
</div></div>
                            </div>
                        </div>

1 个答案:

答案 0 :(得分:1)

您可以尝试不使用jQuery的解决方案,如下所示:

links[1].click(); 
iimPlayCode(
    "WAIT SECONDS=2" + "\n" +
    "TAG POS=1 TYPE=TBODY ATTR=ID:candidatesList" + "\n" +
    "TAG POS=R-1 TYPE=* ATTR=*" + "\n" +
    "TAG POS=R1 TYPE=TD ATTR=TXT:*john*" + "\n" +
    "TAG POS=R1 TYPE=BUTTON ATTR=TXT:Vote" + "\n"
);