[英]Append an Input Field Value to a URL on a .cshtml Page
我在.cshtml
頁面中有一個指向SSRS報告的鏈接:
<div class="col-md-8"> <a href="http://xxx.xx.xx.x/ReportServer/Pages/ReportViewer.aspx?%2fCompany_Reporting%2fpayr0001&rs:Command=Render&StartDate=01/09/2017" target="_blank" >Timesheet Status Report</a> </div>
我需要將日期控件中的值作為參數傳遞,而不是上面的硬編碼日期:
<div class="form-group"> <input type="date" class="form-control" id="datefrom"> </div>
我嘗試將onclick=myfunction()
附加到url以添加日期值,但無法使其正常工作。
function getStartDate() {
return document.getElementById("datefrom").value;
}
任何幫助將非常感激。
干杯。
在日期更改屬性上創建錨標記的href。 隨時隨地建立href。
$('#datefrom').change(function(){
$('#IdOfAnchorTag').attr('href','http://xxx.xx.xx.x/ReportServer/Pages/ReportViewer.aspx?%2fCompany_Reporting%2fpayr0001&rs:Command=Render&StartDate=' + $('#datefrom').val() );
});
相應地檢查字符串。
據我所知,這就是您想要做的(重復的問題?):
附加到網址並刷新頁面
為了以正確的格式獲取日期,您必須在函數主體中執行以下操作:
new Date($('input[type=date]').val()).toLocaleFormat();
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleFormat
另外,您可能要使用'onblur'/ blur事件作為日期輸入,或將事件處理函數連接到專門用於此目的的新按鈕。 這是因為onclick / click事件將在您單擊日期輸入后立即觸發(這可能會打開某種日歷小部件,具體取決於瀏覽器和瀏覽器版本)。
在將日期值傳遞給url之前,請嘗試將日期轉換為dd-MM-yyyy
或MM-dd-yyyy
而不是dd/MM/YYYY
。 因為我認為當url時會誤解“ /”,所以您應該替換為“-”。 然后,如有必要,您可以轉換回舊格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.