[英]How can I get the selected time from my timepicker and assign it to a TextBox value?
我試圖從我的時間選擇器中獲取選定的時間,然后將其分配為@ Html.TextBox()值屬性中的值。 我嘗試使用以下代碼行:@(timeValue)= timepicker('getTime')…,但它不會在timeValue變量中存儲值。 然后,我嘗試使用timeValue = timepicker('getTime')並收到有關未定義datepicker的錯誤消息。 下面的代碼工作正常,除了需要從時間選擇器中獲取選定值的部分。
如何從時間選擇器中獲取所選時間並將其分配給TextBox值? 感謝您對此的任何幫助。
string date = "date" + course.Index.ToString();
string time = "time" + course.Index.ToString();
string timeValue = "";
<script type="text/javascript">
$(function () {
$('input[name= @(date) ]').datepicker();
$('input[name=@(time)]').timepicker({});
@(timeValue) = timepicker('getTime');
});
</script>
string day = DateTime.Now.ToShortDateString();
@Html.TextBox(name: time, value: timeValue, htmlAttributes: new { id = time, @class = "TextBoxCss" })
您無法將javascript分配給razor變量。 而是使用jquery來分配值。
但首先,我認為您沒有使用@ Html.TextBox。
嘗試,
@Html.TextBox("time", null, new {id = "time", @class = "TextBoxCss"})
然后是jQuery,
var timeValue = timepicker('getTime');
$("#time").val(timeValue);
編輯:
使用jquery,ajax形式進行序列化。 例如
$("#sendTimeButton").on("click", function () { // or use on form submit
$.ajax({
type: "POST", // or GET
url: "@Url.Action("Edit", "ControllerName")",
data: $("#myForm").serialize(), //you can use json also JSON.Stringtify
cache: false,
success: function () {
},
error: function () {
console.log("error");
}
});
});
您還可以使用數組:
var timeArray = new Array();
$(".TextBoxCss").each(function(){
timeArray.push($(this).val()) // loop through all text boxes with class "TextBoxCss"
});
然后數據部分是這樣的:
data: {times: timeArray}, // "times" refer to the argument in your controller
然后在您的控制器中:
public ActionResult Edit(int id, string[] times)
所以我把代碼保持得很像我的原始帖子。
string date = "date" + course.Index.ToString();
string time = "time" + course.Index.ToString();
<script type="text/javascript">
$(function () {
$('input[name= @(date) ]').datepicker();
$('input[name=@(time)]').timepicker({});
});
</script>
string day = DateTime.Now.ToShortDateString();
@Html.TextBox(name: time, value: "Set Time", htmlAttributes: new { id = time, @class = "TextBoxCss" })
@Html.TextBox(name: date, value: day, htmlAttributes: new { id = date, @class = "TextBoxCss" })
然后在我的控制器中執行此操作,然后創建一個循環以遍歷列表中的所有texbox:
[HttpPost]
public ActionResult Edit(int id, FormCollection formCollection)
{
String theTimes = Convert.ToString(formCollection["time0"]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.