Javascript 使用post数据重定向到div

Javascript 使用post数据重定向到div,javascript,c#,razor,Javascript,C#,Razor,我正在根据用户输入搜索表中的特定值,用户输入查询数据库中的类似条件。这非常有效,但是我必须手动滚动到页面的按钮才能看到我的过滤表。我真的想将用户重定向到页面下表的div。这是带有搜索框的表单: <form method="post"> <div class="col-lg-6 pull-right" style="margin-right:130px; width:30%;"> <div class="input-group">

我正在根据用户输入搜索表中的特定值,用户输入查询数据库中的类似条件。这非常有效,但是我必须手动滚动到页面的按钮才能看到我的过滤表。我真的想将用户重定向到页面下表的div。这是带有搜索框的表单:

<form method="post">
    <div class="col-lg-6 pull-right" style="margin-right:130px; width:30%;">
        <div class="input-group">
            <input required type="text" class="form-control" name="search" placeholder="Search Alerts for Today...">
            <span class="input-group-btn">
                <button class="btn btn-default" name="searchnow" type="submit" value="Submit">Go!</button>
            </span>
        </div>
    </div>
</form>

使用Response.Redirect将表刷新回其原始状态。如上所示注释掉响应重定向允许过滤器成为可能,但我必须手动向下滚动页面。我想让它重定向到重定向中div的id。请问我能做些什么?

我想您正在做一次完整的服务器往返旅行。在我看来,这是没有必要的。 我建议通过AJAX实现这一点

  • 如下更改HTML以调用按钮上的AJAX操作单击:

    <form method="post">
        <div class="col-lg-6 pull-right" style="margin-right:130px; width:30%;">
            <div class="input-group">
                <input required type="text" class="form-control" name="search" id="search" placeholder="Search Alerts for Today...">
                <span class="input-group-btn">
                    <button class="btn btn-default" name="searchnow" id="theButton">Go!</button>
                </span>
            </div>
        </div>
    </form>
    

  • 你好,丹尼尔,谢谢你的回答。我刚刚编辑了问题中的重定向,因为它们是一个打字错误。将其从“Response.Redirect”(“Dashboard.cshtml#contact”)更改为至“Response.Redirect(“Dashboard.cshtml#search”);”因为这是我要重定向到的div的id的名称。我已将锚定标记改为搜索而不是联系人,将URL改为“Dashboard.cshtml”,但这会很好地过滤表,但不会重定向到div。是否检查了我的HTML?我添加了一个
    id=“search”
    是的,我复制了你的HTML。它仍然不会重定向到div,但会过滤表orry。成功处理程序应该类似于
    $('#search').gotoAnchor()我已经更新了答案我已经在之前的代码中更正了这一点,但仍然不起作用。
    
    <form method="post">
        <div class="col-lg-6 pull-right" style="margin-right:130px; width:30%;">
            <div class="input-group">
                <input required type="text" class="form-control" name="search" id="search" placeholder="Search Alerts for Today...">
                <span class="input-group-btn">
                    <button class="btn btn-default" name="searchnow" id="theButton">Go!</button>
                </span>
            </div>
        </div>
    </form>
    
    $.fn.gotoAnchor = function(anchor) {
        location.href = this.selector;
    }
    
    $(document).ready(function() {
        // Add the page method call as an onclick handler for the div.
        $("#theButton").click(function() {
            $.ajax({
                type: "POST",
                url: "<YOUR URL>",
                data: { search: $('#search').val() },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    // If everything is successful link to your anchor tag
                    $('#search').gotoAnchor();
                }
            });
        });
    });