Javascript 更改输入时的更改编辑器
当我更改输入时,我想更改编辑器以显示数据库中已有的数据 我将获取输入的输出并将其拆分,然后将结果放入2个编辑器中Javascript 更改输入时的更改编辑器,javascript,html,asp.net-mvc-4,Javascript,Html,Asp.net Mvc 4,当我更改输入时,我想更改编辑器以显示数据库中已有的数据 我将获取输入的输出并将其拆分,然后将结果放入2个编辑器中 <input type="text" value="Set Date" id="reservationtim onchange=myFunction()> @Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control" }, id = "
<input type="text" value="Set Date" id="reservationtim onchange=myFunction()>
@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control" }, id = "startDate" })
@Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control" } , id = "endDate" })
您正在尝试将元素引用本身设置为所需的值。相反,您需要设置元素引用的值属性:
document.getElementById("startDate").value = res[0];
另外,这里看起来您正在尝试混合服务器端和客户端代码。请记住,只有在服务器处理完文档并返回给客户端之后,才会处理JavaScript。一旦进入客户端,就只剩下DOM,它是由浏览器根据服务器发送的HTML文档创建的。换句话说,您使用的EditorFor
或任何东西都完全丢失了。您所拥有的只是调用EditorFor
的结果(即HTML),您应该首先更正HTML,因为某些属性没有正确指定
<input type="text" value="Set Date" id="reservationtime" onchange="myFunction()">
@Html.EditorFor(model => model.StartDate, new { @class = "form-control" } )
@Html.EditorFor(model => model.EndDate, new { @class = "form-control" } )
@EditorFor(model=>model.StartDate,new{@class=“form control”})
@EditorFor(model=>model.EndDate,新的{@class=“form control”})
我不知道为什么要显式地为上述两个编辑器设置id。默认情况下,它们将属性名作为ID。所以我建议使用下面的脚本
<script>
function myFunction() {
var date = document.getElementById("reservationtime").value;
var res = date.split("-");
model => model.StartDate = res[0];
if(res.length>1) {
document.getElementById("StartDate").value = res[0];
document.getElementById("EndDate").value = res[1];
// if you still want to set ID properties instead of using default use following
// document.getElementById("StartDate").value = res[0];
// document.getElementById("EndDate").value = res[1];
}
}
</script>
函数myFunction(){
var date=document.getElementById(“reservationtime”).value;
var res=拆分日期(“-”);
model=>model.StartDate=res[0];
如果(分辨率长度>1){
document.getElementById(“StartDate”).value=res[0];
document.getElementById(“EndDate”).value=res[1];
//如果仍要设置ID属性而不是使用默认属性,请使用以下命令
//document.getElementById(“StartDate”).value=res[0];
//document.getElementById(“EndDate”).value=res[1];
}
}
<script>
function myFunction() {
var date = document.getElementById("reservationtime").value;
var res = date.split("-");
model => model.StartDate = res[0];
if(res.length>1) {
document.getElementById("StartDate").value = res[0];
document.getElementById("EndDate").value = res[1];
// if you still want to set ID properties instead of using default use following
// document.getElementById("StartDate").value = res[0];
// document.getElementById("EndDate").value = res[1];
}
}
</script>