我在foreach
中有html元素。我無法手動向這些人聲明ID名稱。 我用id='[email protected](item.Id)'
設置編號:如何設置ID並在mvc 4剃鬚刀中動態使用它?
foreach (var item in Model)
{
<a href="#" id='[email protected](item.Id)' class="select-link">Select this</a>
}
不過,現在我需要。點擊()功能eash <a>
鏈接做我的一些操作。如何爲動態ID編寫點擊函數?
我檢查了這一點,但它不工作:
$("#[email protected](item.Id)").click(function() {
//any operation
});
編輯:
<script>
$(document).ready(function() {
$('.select-link').click(function() {
var id = $(this).attr('data-id');
url = '@Url.Action("SelectThis", "Product")';
var data = { id: '@Model.Id' };
$.post(url, data, function (result) {
if (result.success) {
alert("Thanks");
}
else {
alert("Problem occured...");
}
});
});
});
</script>
此點擊()函數發送請求到控制器迭代計數倍。我的代碼有什麼問題?
'$('a')。click();'可以做到這一點。其實你的目的不明確。爲什麼你只想選擇特定的ID做這些東西? – Jai
例如,asume,在http://stackoverflow.com中回答accept結構。我需要的想法是類似的,從動態排列的答案列表中選擇任何項目。 –