如何从时间选择器中获得所选时间并将其分配给TextBox值?
本文关键字:时间 分配 TextBox 选择器 | 更新日期: 2023-09-27 17:50:22
我试图从我的时间选择器中获得选定的时间,然后将其分配为@Html.TextBox()值属性中的值。我尝试过使用这行代码:@(timeValue) = timepicker('getTime')…但它不会在timeValue变量中存储值。然后我尝试timeValue = timepicker('getTime'),并得到关于未定义日期picker的错误消息。下面的代码工作得很好,除了我需要从我的时间选择器中获得所选值的部分。
我如何从我的时间选择器中获得所选时间并将其分配给文本框值?谢谢你对这个问题的帮助。
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" })
所以我保持代码非常像我原来的帖子....
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" })
然后在我的控制器中这样做我可以创建一个循环来遍历列表中的所有文本框:
[HttpPost]
public ActionResult Edit(int id, FormCollection formCollection)
{
String theTimes = Convert.ToString(formCollection["time0"]);
}
你不能将javascript赋值给razor变量。使用jquery来赋值。
但首先,我认为你没有使用@Html。文本框。
,
@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)