2015-07-21 24 views
0

尋找一些關於如何捕獲特定TD單元的值的幫助。我面臨的挑戰是我無法修改TD單元以包含類或修改它們,因爲表格內容是由另一個系統動態創建的。從TD單元捕獲文本並將變量連接到url

我在下面創建了小提琴,給你一個什麼樣的表結構的想法。正如我上面所解釋的,因爲我不能直接修改表格結構或添加額外的類,以便更容易地定位我想要的TD單元,所以我正在嘗試使用我的方法來取代這個。

我試圖做到這一點是:

1)需要捕獲參考號碼(11總是個字符,並且僅包含數字)。

2)將捕獲的參考號碼放入一個變量中。

3)串聯可變到以下網址http://test.com/UpdateNotification.asp?OEN=(可變這裏)& CL = ALL & SU = ALL

4)更換與所述URL的參考號。

所以我真的有這個過程的第1步的問題。我將不勝感激這方面的一些幫助。

http://jsfiddle.net/W4Km8/5665/

<DIV class="sm_content"> 
    <table border="1" width="100%"> 
     <tr> 
      <td class="ms-vb2">reference numbers</td> 
      <td class="ms-vb2">name</td> 
      <td class="ms-vb2">Description</td> 
      <td class="ms-vb2">Incident</td> 
     </tr> 
     <tr> 
      <td class="ms-vb2">20150715110</td> 
      <td class="ms-vb2">Jhonson Doe</td> 
      <td class="ms-vb2">Box of cereal and other stuff</td> 
      <td class="ms-vb2">123</td> 
     </tr> 
     <tr> 
      <td class="ms-vb2">20150715111</td> 
      <td class="ms-vb2">Bobby Boucher</td> 
      <td class="ms-vb2">Box of nails and stuff</td> 
      <td class="ms-vb2">124</td> 
     </tr> 
    </table> 
</DIV> 

回答

0

在未來,請詢問之前作出努力。也就是說,編碼人員需要編碼。

$('.sm_content table tr:gt(0)').each(function() { 
    var myId = $(this).find('td').eq(0).text(); 
    var myUrl = 'http://test.com/UpdateNotification.asp?OEN=' + myId; 
    $(this).find('td').eq(0).html('<a href="' + myUrl + '">' + myId + '</a>'); 
}); 

Fiddle demo

或者,如果你沒有真正想要的ID聯繫,只是這樣做的最後一行:

$(this).find('td').eq(0).text(myUrl); 
+0

非常感謝代碼伊舍伍德,這對我很有用。但是,如果沒有條目來顯示錶格輸出,則會顯示一條消息,指出無法顯示該消息並使其成爲URL。你有什麼想法如何防止這種情況?下面是一個例子http://jsfiddle.net/W4Km8/5672/ –

+0

簡短的回答是,你可以使用一個正則表達式來檢測非數字字符和一個'if'語句來過濾操作。如果您需要幫助,請提出一個新問題。 – isherwood

+0

當然,再次感謝 –

-1

加載整個頁面爲一個字符串。我用phpfilegetcontents但對於一個HTML頁面只有我手動複製和頁面粘貼到文本框,並使用document.getElementbyid('box').innerHTML頁面的內容加載到一個字符串

找到第一個單元格:entirepage.indexOf('td')

entirepage.slice(start from location of td plus enough characters to get past the rest of the tag, end 11 characters later) 這就是你的參考號 找到'td' 等等的第n個實例。

不需要jquery

+1

儘管這可能會在理論上回答OP的問題,但在您的解決方案的代碼中提供示例會很有幫助 - 因爲很難準確理解您的建議。 – brandonscript

0

你有不同的方式來做到這一點。

(function($){ 
    $('.sm_content tr').slice(1).each(function(){ 
     var td = $(this).find('td').first(); 
     var tdContent = td.html(); 
     td.html('<a href="http://test.com/UpdateNotification.asp?OEN=' + tdContent + '&CL=ALL&SU=ALL" alt="' + tdContent + '">' + tdContent + '</a>'); 
    }); 
})(jQuery); 
相關問題