2016-03-08 57 views
0

如果單擊顯示列表中的第一個值的按鈕,我試圖獲得警報。我使用下面的代碼基於這個threat。在這種威脅中,他們使用的不是一張表格。但我認爲那不是問題......我認爲現在是這樣。 I M使用的代碼是這樣的(其特點是從openlayer地圖):未被捕獲的參考錯誤:來自列表中的值的alertbox

<ul id ="table_overvieuw"> 
<#list features as feature> 
    <li><b>Type: ${type.name}</b> (id: <em>${feature.fid}</em>): 
    <ul> 
    <#list feature.attributes as attribute> 
    <#if !attribute.isGeometry> 
     <li><b>${attribute.name}</b></li> 
     ${attribute.value} 
    </#if> 
    </#list> 
    <br><br> 
<input type="button" name="geef slijpplaten info" class="ok" value="OK" onclick="myFunction()" /> 
    </ul> 
    </li> 
</#list> 
</ul> 
<script type="text/javascript"> 
function highlight(e) { 
    if (selected[0]) selected[0].className = ''; 
    e.target.parentNode.className = 'selected'; 
} 

var table = document.getElementById('table_overvieuw'), 
    selected = table.getElementsByClassName('selected'); 
table.onclick = highlight; 

function myFunction(){ 

    alert($("li.selected li:first").html()); 
} 

</script> 

I M在控制檯得到的錯誤是:未捕獲的ReferenceError:$沒有定義。 $是從這段代碼:

alert($("li.selected li:first").html()); 

有人能幫我一個忙嗎?

+3

'$('...')'是jquery選擇器,是否在您的html頁面中包含了jquery? – Nadir

+0

謝謝!我沒有意識到我使用它 – waywer

回答

1

要使用jquery,你必須在你的頁面上擁有它。 使用CDN(http://jquery.com/download/#using-jquery-with-a-cdn)。 您也可以與涼亭安裝(涼亭中安裝jQuery)和頁面上的鏈接是:如果被正確地形成列表框,然後嘗試下面的代碼的警告框裏面

<html> 
<head> 
    <script src="/bower_components/jquery/dist/jquery.min.js"></script> 
... 
</head> 
... 
</html> 
+0

或者您可以使用 。不需要安裝 – waywer

0

: -

$('ul#table_overview li:first').text() 
+0

我需要把它放在哪裏? – waywer

+0

函數myFunction(){ alert($(「li.selected li:first」).html()); } 用上面的代碼 – Webdev

+0

替換現有的代碼,你能向我解釋它的作用嗎?因爲我填充了它(警告$),並且警報沒有任何響應 – waywer