首页 > 解决方案 > 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 ();

       }

'''

标签: asp.net-web-api

解决方案


您不需要为任何客户端处理调用 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>

推荐阅读