2015-11-17 90 views
1

我有一些代碼,允許用戶輸入數據到引導日期選擇器,然後單擊提交按鈕和日期顯示在navpanel內。在按鈕上單擊我有一個JavaScript函數,將確定用戶在哪個選項卡上,並將在MVC控制器中運行C#函數。我目前有兩個函數DelayedSpiffDate,InstantSpiffDate。出於某種原因,即使選中了instantspiff選項卡,它也只會加載DelayedSpiffDate函數。我不確定我做錯了什麼?這裏是我的html:Bootstrap navpanel和日期選擇器JavaScript調用C#控制器

<div class="row-fluid 1"> 
    <div class="col-lg-12 delayed_spiff_body"> 

     <div class="row spiff-datepicksection"> 
      <div class="col-lg-6 pull-right"> 
       <div class="col-sm-5 col-lg-offset-4"> 
        <div class="form-group"> 
         <div class="input-group date"> 
          <input type="text" id="startDate" class="form-control" /> 
          <span class="input-group-addon"> 
           <span class="glyphicon glyphicon-calendar"></span> 
          </span> 
         </div> 
        </div> 
       </div> 
       <div class="col-lg-3"> 
        <input class="spiffdate-btn" type="button" value="Submit" /> 
       </div> 
      </div> 
     </div> 


     <div class="row spiff_tabs_body"> 
      <!-- Nav tabs --> 
      <ul class="nav nav-tabs spiff_tabs" role="tablist"> 
       <li role="presentation" class="active"> 
        <a href="#home" aria-controls="home" role="tab" data-id="delayedspiff" data-toggle="tab" onclick="FormGet('dashboard/delayedspiff', 'delayedspiff')">Potential Spiff</a> 
       </li> 
       <li role="presentation"> 
        <a href="#profile" aria-controls="profile" role="tab" data-id="instantspiff" data-toggle="tab" onclick="FormGet('dashboard/instantspiff', 'delayedspiff')">Instant Spiff</a> 
       </li> 
      </ul> 
      <!-- Tab panes --> 
      <div class="tab-content"> 
       <div role="tabpanel" class="tab-pane active" id="delayedspiff"></div> 
       <div role="tabpanel" class="tab-pane" id="instantspiff"></div> 
      </div> 
     </div> 
    </div> 
</div> 

下面是我的javascript:

$(".spiffdate-btn").click(function(){ 
var correctid = $(".tab-content .active").attr("id"); 
var startDate = $("#startDate").val(); 
if (correctid == "delayedspiff") 
    $.get("@Url.Action("DelayedSpiffDate", "Dashboard")", { startDate: startDate }); 
else 
    $.get("@Url.Action("InstantSpiffDate", "Dashboard")", { startDate: startDate }); 
}); 

更新的javascript:

$(".spiffdate-btn").click(function(){ 
    var correctId = $("ul.spiff_tabs li.active a").attr('data-id'); 
    console.log(correctId); 

    var startDate = $("#startDate").val(); 

    if (correctId == "delayedspiff") 
    {   
     $.get("@Url.Action("DelayedSpiffDate", "Dashboard")", { startDate: startDate }); 

    } else if (correctId == "instantspiff") { 

     $.get("@Url.Action("InstantSpiffDate", "Dashboard")", { startDate: startDate }); 
    }  

});

+0

檢查你是否在'correctid'中添加了正確的id,比如'condition.log(correctid)'前面的條件。 –

+0

@ ZakariaAcharki我沒有得到糾正的正確ID。即使選中了instantspiff選項卡,它也會顯示delayedspiff。 – hollyquinn

+0

我收到了正確的ID。問題是$ .get應該失敗,因爲第一個參數是多個字符串而不是一個封裝。 – Brant

回答

1

如果你想讓它基於點擊選項卡上您指定了錯誤的項目.. 。試試這個作爲你的'getter':

var correctId = $("ul.spiff_tabs li.active a").attr('data-id'); 
+0

當我將其更改爲單引號時,出現了扭曲的線條和錯誤消息,就像你有。錯誤是字符文字中的字符過多。我不確定逃跑是什麼?我很新的JavaScript。 – hollyquinn

+0

好吧,我試過這個。現在,當我點擊按鈕沒有發生什麼事情?我複製了我的新JavaScript代碼,其中包含您在我的問題中提出的建議。我究竟做錯了什麼? – hollyquinn

+0

我在console.log中找到了正確的id。我不知道爲什麼它沒有運行Url.Action。它永遠不會讓它成爲C#代碼。 – hollyquinn

0

在這一點上改變你的代碼,你在FormGet秒參數是錯誤的,它從delayedspiffinstantspiff改變:

<ul class="nav nav-tabs spiff_tabs" role="tablist"> 
    <li role="presentation" class="active"> 
     <a href="#home" aria-controls="home" role="tab" data-id="delayedspiff" data-toggle="tab" onclick="FormGet('dashboard/delayedspiff', 'delayedspiff')">Potential Spiff</a> 
    </li> 
    <li role="presentation"> 
     <a href="#profile" aria-controls="profile" role="tab" data-id="instantspiff" data-toggle="tab" onclick="FormGet('dashboard/instantspiff', 'instantspiff')">Instant Spiff</a> 
    </li> 
</ul> 
+0

馬庫斯我做到了這一點,但它仍然無法正常工作。無論選擇哪個選項卡,它仍會運行delayedspiffdate函數。 – hollyquinn

+0

她沒有發佈formGet()的代碼,所以我們不知道第二個arg在那個函數中做了什麼。 – Brant

+0

@Brant formGet()函數只是加載標籤內的表單。這與我現在的工作沒有任何關係。 – hollyquinn