gpt4 book ai didi

asp.net-core - 如果值发生变化,如何调用选择列表中的函数或方法?

转载 作者:行者123 更新时间:2023-12-02 14:46:51 25 4
gpt4 key购买 nike

我有一个场景,如果我更改选择列表的值,我希望能够清除模型或任何我想清除的值。是否可以在 select 标签助手中调用函数或方法?像这样:

<select asp-for="Input.DriverName" class="form-control"
asp-items="Model.DriverNotUser"
onchange="myfunction()">
</select>

最佳答案

调用 javascript 函数:

<select class="form-control" 
asp-for="DriverName"
asp-items="Model.DriverNotUser"
onchange="myfunction();">
</select>

@section Scripts{
function myfunction() {
// Do whatever...
}
}

要提交到后端,将选择列表放在一个表单中并调用提交:

<!-- submit to the OnGet() method -->
<!-- add method="post" to submit to the OnPost() method -->
<form>
<select class="form-control"
asp-for="DriverName"
asp-items="Model.DriverNotUser"
onchange="this.form.submit();">
</select>
</form>

在后端调用自定义方法:

<!-- submit to the OnPostMyMethod() method -->
<form method="post" asp-page-handler="MyMethod">
<select class="form-control"
asp-for="DriverName"
asp-items="Model.DriverNotUser"
onchange="this.form.submit();">
</select>
</form>

后端方法:

public IActionResult OnPostMyMethod()
{
// ...
}

关于asp.net-core - 如果值发生变化,如何调用选择列表中的函数或方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58618679/

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