2016-08-03 75 views
0

我有按鈕和動態生成的下拉菜單。我想在頁面加載時訪問下拉的ID,但自動生成的ID只會給我第一個下拉的ID(請看腳本)。並請,如果你能告訴我如何縮進代碼正確如何在asp.net mvc中獲取自動生成的多個控件的id?

<div class="row"> 
<div class="col-md-9"> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots) 
{ 
if (slot.day == "Monday"){ 
<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal" data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button> 
</div> 
}} 
</div> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots) 
{if (slot.day == "Tuesday") 
{<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal"data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button> 
</div> 
} 
} 
</div> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots) 
{if (slot.day == "Wednesday") 
{ 
<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal" data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button> 
</div> 
} 
} 
</div> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots){ 
if (slot.day == "Thursday"){ 
<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal" data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button></div> 
}} 
</div> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots){ 
if (slot.day == "Friday"){ 
<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal" data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button> 
</div> 
}} 
</div> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots){ 
if (slot.day == "Saturday") { 
<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal" data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button></div> 
} 
} 
</div> 
<div data-id="@data.id" id="[email protected]"> 
@foreach (var slot in data.avail_slots){ 
if (slot.day == "Sunday"){ 
<div class="col-md-3 col-xs-4" style="line-height:2em;"> 
<button id="appointment_click" data-toggle="modal" data-target="#confirmation" style="margin-bottom:4px" class="pull-left btn btn-pill btn-primary">@slot.time</button></div>}} 
</div> 
</div>   
<div class="col-md-3"> 
<select data-id="@data.id" id="[email protected]" class="temp"> 
@foreach (var d in ad_list){ 
<option value="@d.id">@d.day_name</option>} 
</select> 
</div> 

<script> 
var today_number = new Date().getDay(); 
var today = get_all(today_number) 
$('select>option:eq(' + today_number + ')').attr('selected', true); 
var id = $('.temp').attr('data-id'); 


</script> 
+0

做所有下拉具有相同的ID? –

+0

我正在使用數據ID給他們不同的ID。 我無法在網頁加載時訪問它 – Zawar

回答

0

<select data-id="@data.id" id="[email protected]" class="temp">語句是foreach循環之外。如果你打算生成多個下拉菜單,然後把select語句foreach循環裏面,如下:

@foreach (var d in ad_list) 
{ 
    <select data-id="@data.id" id="[email protected]" class="temp"> 
     <option value="@d.id">@d.day_name</option> 
    </select> 
} 
-1

你需要看看在頁面有很多相同的ID的瞬間生成的ID的存在這將導致你的問題。

但是隻有一個ddl,你不需要動態生成的id。它在你的循環之外。

如果您確實需要動態生成的Id看看here

相關問題