简体   繁体   English

JavaScript函数未在ASP.NET MVC的部分视图中触发

[英]JavaScript Function Is Not Firing in Partial View In ASP.NET MVC

I have added some javascript function in my partial page. 我在部分页面中添加了一些javascript函数。 But it is not firing. 但这不是射击。 Need help. 需要帮忙。

Thanks in advance 提前致谢

@section scripts {
<script type="text/javascript">
$(document).ready(function () {


    $('#btn').click(function (event) {
        alert("Hi");
        var filterstring = $('#txtCode').val();
        $.get("/Test/Details?filterstring=" + $('#txtCode').val() + "", function (data) {
            var getHTML = $(data);
            $('#WebGrid').empty();
            $('#WebGrid').html($('#WebGrid', getHTML));
            ChkAddClass();
        });
    });


</script>
   }

It is not possible to use sections in a partial view , you have to move this script to main view or use a custom helper. 无法在局部视图中使用 ,您必须将此脚本移至主视图或使用自定义帮助器。

And also since you are using AJAX you have to use delegated events to attach an event handler. 而且,由于您正在使用AJAX,因此必须使用委托事件来附加事件处理程序。

Please read the Direct and delegated events section of .on() . 请阅读.on()直接事件和委派事件

$(document).on('click', '#btn', function(){
    alert("Hi");
    var filterstring = $('#txtCode').val();
    $.get("/Test/Details?filterstring=" + $('#txtCode').val() + "", function (data) {
        var getHTML = $(data);
        $('#WebGrid').empty();
        $('#WebGrid').html($('#WebGrid', getHTML));
        ChkAddClass();
    });
});

Thanks! 谢谢!

You are closing you doc ready block after the closing script tag and you are missing ); 您要在关闭脚本标记后关闭doc ready块,并且缺少);

<script type="text/javascript">
$(document).ready(function () {


    $('#btn').click(function (event) {
        alert("Hi");
        var filterstring = $('#txtCode').val();
        $.get("/Test/Details?filterstring=" + $('#txtCode').val() + "", function (data) {
            var getHTML = $(data);
            $('#WebGrid').empty();
            $('#WebGrid').html($('#WebGrid', getHTML));
            ChkAddClass();
        });
    });

});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM