asp.net-web-api - Web API Clear Button
问题描述
Trying to get a clear button to work for my simple calculator. It should clear three text boxes: the first number (fn), second number (sn), and the total (tn). I am using a web api for this - everything works except for the clear button. I am uncertain on how to write the Clear function correctly - any guidance would be appreciated. I think I am going in the wrong direction with the controller function. Below I listed my HTML, then my Controller, and then my model for just the clear function.
HTML:
'''
function clr() {
// clear function goes here
$.ajax({
url: "/api/calc/clr/" + fn.value + "/" + sn.value,
cache: false,
success: function (html) {
tn.value = html
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
</script>
<input type="reset" value="Clear Results" onclick="clr();" />
Controller code:
[Route("api/calc/clr/{paramOne}/{paramTwo}")]
public IHttpActionResult Delete(float paramOne, float paramTwo)
{
return oCalc.Clr(paramOne, paramTwo);
}
Model Code:
public float Clr (float fn, float sn)
{
return ();
}
'''
解决方案
您不需要为任何客户端处理调用 API。您可以使用以下代码清除值
<input type="text" id="n1" value="10" />
<input type="text" id="n2" value="20" />
<input type="button" onclick="clr()" />
JavaScript 代码
<script>
function clr() {
// clear function goes here
$("#n1,#n2").val("");
}
</script>
推荐阅读
- javascript - 我该如何解决这个问题?
- django - 如何查看 Django connection.cursor.execute() 添加了哪些行?
- bash - 对 bc 的结果使用 expr
- c++ - 尝试从单链表 C++ 中删除单个节点时发生读取访问冲突
- sql - 在 PostgreSQL 中使用 DATE_TRUNC 计算一周的结束日期
- azure-devops - Azure DevOps - 我可以按项目分隔工作项自动 ID 编号吗?
- html - 我想为我的网站加载我的简历的 html 版本。但它无法编译
- java - 为什么 3 sum 算法只查看特定数字右侧的子数组?
- java - 即使在运行配置中指定,Eclipse 也无法找到 adb
- python - 如何遵循提示:改用可调用对象,例如,使用 `dict` 代替 `{}`?