gpt4 book ai didi

单击按钮时,javascript 函数未定义错误

转载 作者:行者123 更新时间:2023-11-28 15:55:43 24 4
gpt4 key购买 nike

想象一下下面的 MVC 父模型:

型号:

Namespace MyProject.SampleModel
{
public class ViewModelExample {
public FirstModel BoolValues { get; set; }
public SecondModel NamesValues { get; set; }
}
}

Namespace MyProject.SampleModel
{
public class FirstModel
{
public bool MyBoolean1 { get; set; }
public bool MyBoolean2 { get; set; }
}

public class SecondModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}

查看:

@model MyProject.SampleModel.ViewModelExample

@using (Html.BeginForm("MyAction", "MyController", FormMethod.Post, htmlAttributes: new { id = "Myform" }))
{

(...)

@Html.CheckBoxFor(m => m.BoolValues.MyBoolean1)

(...)

<input id="submitButton" type="button" value="Add" onclick="InitiateSequence();" />

(...)
}

<script type="text/javascript" src="~/Scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript">

(...)

function InitiateSequence()
{
// Do some stuff
}

(...)
function ScriptSample() {

if(@(Model.BoolValues.MyBoolean1 ? "true" : "false")
{
// It's true, do some stuff
}
else
{
// It's false, do some stuff
}
}

</script>

Controller :

public ActionResult MyAction(ViewModelExample model)
{
model.FirstModel = new TestsModel(); // I do not instantiate SecondModel as in the view for this controller i do not use it

return View(model);
}

页面加载正确,但是当我单击按钮时,它显示 javascript 函数 InitiateSequence 未定义......发生了什么事?

最佳答案

这可能是因为该函数很可能出现在不应该出现的地方。也不要使用内联属性来绑定(bind)处理程序,使用事件绑定(bind)而不是内联处理程序。

<input id="submitButton" type="button" value="Add" />

<script type="text/javascript">

(...) //whatever code

$(function(){
$('#submitButton').on('click', InitiateSequence);
});

关于单击按钮时,javascript 函数未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18937505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com