在我的項目一個HTML元素的二代身份證,在JSP頁面中的一個有這樣的HTML結構:如何識別與多個ID
<table id="hor-minimalist-a" class="campos">
<thead>
<tr>
<th>Campo</th>
<th>#</th>
</tr>
</thead>
<tfoot>
<tr>
<td> <input type="text" name="nome_campo"> </td>
<td> <button type="button" id="incluir_campo" class="btn btn-link">Incluir</button> </td>
</tr>
<tr>
<td> <div id="result_incluir_campo"></div> </td>
<td> <div id="result_excluir_campo"></div> </td>
</tr>
</tfoot>
<c:forEach var="item_key" items="${campos}">
<tr id="linha_campo_${item_key}">
<td> <input type="text" value="${item_key}"> </td>
<td> <button type="button" id="excluir_campo_${item_key}" class="btn btn-link">Excluir</button> </td>
</tr>
</c:forEach>
</table>
注行:
<button type="button" id="excluir_campo_${item_key}" class="btn btn-link">Excluir</button>
我有一個jQuery函數與之關聯:
<c:forEach var="item_key" items="${campos}">
<script>
$("#excluir_campo_${item_key}").on("click", function() {
$.ajax({
type: "GET",
url: "<c:out value="${pageContext.request.contextPath}/key/remove_campo"/>",
cache: false,
data: {nome: "${item_key}"}
}).done(function(data){
if(data == "yes") {
$("#linha_campo_${item_key}").remove();
}
else if(data == "not"){
$("#result_excluir_campo").empty().append("erro");
}
else {
$("#result_excluir_campo").empty().append("sem acesso");
}
});
});
</script>
</c:forEach>
我使用JSTL,但我面臨的一些問題,這個解決方案,因爲我的列表可以upda動態地。
採取在考慮我從此元素更改ID:
<button type="button" id="excluir_campo_${item_key}" class="btn btn-link">Excluir</button>
這個兩(分離電流Id的兩個「條款」):
excluir_campo ${item_key}
有什麼辦法來檢測在secong ID與jquery的功能類似於:
$("#excluir_campo").on("click", function() {
var second_id = ???;
$.ajax({
type: "GET",
url: "<c:out value="${pageContext.request.contextPath}/key/remove_campo"/>",
cache: false,
data: {nome: "<second_id>"}
}).done(function(data){
if(data == "yes") {
$("#linha_campo_<second_id>").remove();
}
else if(data == "not"){
$("#result_excluir_campo").empty().append("erro");
}
else {
$("#result_excluir_campo").empty().append("sem acesso");
}
});
});
或者有別的辦法來完成ŧ他上面代碼的結果是一樣的嗎?
使用'class'和數據屬性。 –
你也可以使用CSS選擇器'[id^= excluir_campo_]'。 –
我想使用類的屬性,但我怎麼可以檢索該值來存儲在一個變量(如我在問題中的例子)。 –