2010-05-16 40 views
0

我有以下鏈接。點擊時,我想檢查item.primary_company字段,如果填充,請給用戶一個警告,詢問他們是否想繼續。我怎樣才能做到這一點?如何在基於驗證的ActionLink之前提供用戶確認消息

<a href="<%= Url.Action("Activate", new {id = item.company_id}) %>" class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"></span></a> 

編輯

我已經改變了這一點,但點擊時沒有任何反應。此外,我不知道如何引用該項目來檢查primary_company字段。我只想要顯示item.primary_company.HasValue。我還想在確認消息中顯示item.company1.company_name。

<a href="#" onclick="return Actionclick("<%= Url.Action("Activate", new {id = item.company_id}) %>");" class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"></span></a> 


<script type="text/javascript"> 
function Actionclick(url) 
{ 
    alert("myclick"); 
    if (confirm('Do you want to activate this company\'s primary company and all other subsidiaries?')) 
     { 
      location.href(url); 
     } 

}; 
</script> 

回答

1

在已編輯的示例中的代碼失敗,因爲的雙重使用雙引號的。

如果item.primary_company.HasValue爲真,那麼只顯示公司名稱的確認,它可以在服務器端或客戶端完成。

服務器端,改如何根據狀態的鏈接的作品:

<% if (item.primary_company.HasValue) { %> 
<a href="#" onclick="return Actionclick('<%= Url.Action("Activate", new {id = item.company_id}) %>', '<%= Html.Encode(item.company1.company_name) %>');" 
    class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"></span></a> 
<% } else { %> 
<a href="<%= Url.Action("Activate", new {id = item.company_id}) %>" 
    class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"> 
     link</span></a> 
<% } %> 

<script type="text/javascript"> 
    function Actionclick(url,companyName) { 
     if (confirm('Confirm. CompanyName = ' + companyName)) { 
      location.href = url; 
     } 
    }; 
</script> 

客戶端,發送一個參數的JavaScript,告訴它是否要確認:

<a href="#" onclick="return Actionclick('<%= Url.Action("Activate", new {id = item.company_id}) %>', '<%= Html.Encode(item.company1.company_name) %>', <%= item.primary_company.HasValue ? "true" : "false" %>));" 
    class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"></span></a> 

<script type="text/javascript"> 
    function Actionclick(url,companyName,showConfirmation) { 
     if (showConfirmation) { 
      if (confirm('Confirm. CompanyName = ' + companyName)) { 
       location.href = url; 
      } 
     } 
     else { 
      location.href = url; 
     } 
    }; 
</script> 
+0

非常感謝你的例子。這真的有幫助! – RememberME 2010-05-16 21:34:55

1
<a onclick="return companyClick(\"<%= Html.Encode(item.company_name) %>\");" href="<%= Url.Action("Activate", new { id = item.company_id }) %>" class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"> 
    <span class="ui-icon ui-icon-refresh"></span> 
</a> 

,並在javascript:

function companyClick(companyName) { 
    return confirm(
     'Do you want to activate ' 
     + companyName + 
     ' company's primary company and all other subsidiaries?'); 
} 

而且使用jQuery:

<a href="<%= Url.Action("Activate", new { id = item.company_id }) %>" title="<%= Html.Encode(item.company_name) %>" class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"> 
    <span class="ui-icon ui-icon-refresh"></span> 
</a> 

$(function() { 
    $('a').click(function() { 
     return confirm(
      'Do you want to activate ' 
      + $(this).title + 
      ' company's primary company and all other subsidiaries?'); 
    }); 
}); 

UPDATE:

忘了逃跑company's撇號:

function companyClick(companyName) { 
    return confirm(
     'Do you want to activate ' 
     + companyName + 
     ' company\'s primary company and all other subsidiaries?'); 
} 
+0

作品鏈接,但我沒有收到確認信息。 – RememberME 2010-05-16 20:12:27

+0

對不起,我犯了一個錯誤,沒有逃過公司的撇號。請參閱我的更新。 – 2010-05-16 21:24:40

+0

我自己也說過了。仍然沒有得到確認信息。我在回車確認之前添加了一個「警報」,但也沒有觸發。 – RememberME 2010-05-16 21:30:40

相關問題