代码之家  ›  专栏  ›  技术社区  ›  Newit

Ajax调用仅在webgrid的第一页上工作

  •  0
  • Newit  · 技术社区  · 7 年前

    大家好,

    var Grid = new WebGrid(canPage: true, canSort: false, rowsPerPage: 25, ajaxUpdateContainerId: "gridContent");
    Grid.Bind(Model.DataList);
    Grid.Pager(WebGridPagerModes.NextPrevious);
    
    var gridColumns = new List<WebGridColumn>();
    
    gridColumns.Add(PaymentGrid.Column(columnName: "WavFile", header: " ", format:@<text>
    @if (item.WavFile != null && item.WavFile != "")
    { <a href="#" id="audiobtn" class="audiobtn"><img src="~/Content/audio.png" 
    title="Download" alt="@item.WavFile" /></a> }
    else
    { @Html.Raw("<img src=\"/Content/audiooff.png\" />") }</text>));
    gridColumns.Add(Grid.Column(columnName: "EmployeeID", header: "EmpID"));
    gridColumns.Add(Grid.Column(columnName: "LastName", header: "Last", canSort: 
    true));
    gridColumns.Add(Grid.Column(columnName: "FirstName", header: "First", canSort: true));
    
    <div id="gridContent">
    @Grid.GetHtml(
    tableStyle: "table table-striped table-hover",
    headerStyle: "",
    footerStyle: "",
    alternatingRowStyle: "",
    rowStyle: "",
    mode: WebGridPagerModes.All,
    columns: Grid.Columns(gridColumns.ToArray())
    ) 
    </div>
    
    <script type="text/javascript">
    $(document).ready(function () {
    
    $(".audiobtn").click(function (e) {
    
    $.ajax({
    type: "POST",
    url: "/EmployeeInfo/VoiceAuthorization",
    data: { audio: $(this).find('img').attr('alt') },
    async: false,
    cache: false,
    success: function (response) {
    
    if (e.button == 0) {
    $("#ContextMenu").css('left', e.pageX + 5);
    $("#ContextMenu").css('top', e.pageY + 5);
    $("#ContextMenu").fadeIn(100); 
    } 
    },
    error: function (error) {
    alert('Invalid wav file');
    console.log(error);
    }
    });
    
    return false;
    });
    
    $("#ContextMenu").click(function () {
    $("#ContextMenu").fadeOut(80); 
    });
    
    $(document).click(function (e) {
    if (e.target.id != 'audiobtn' && !$('#audiobtn').find(e.target).length) {
    $("#ContextMenu").hide(); 
    } 
        });
    
        return false;
        });
    });
    

    非常感谢。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Federico Schiocchet    7 年前

    由于第一个页面旁边的页面是动态生成的,因此它不起作用。

    更换代码 $(".audiobtn").click(function (e) { ... }); 具有 $("body").on("click", ".audiobtn", function () { .. });

        2
  •  0
  •   Victor Hugo Terceros    7 年前

    更改此

    <a href="#" id="audiobtn" class="audiobtn"><img src="~/Content/audio.png" 
    title="Download" alt="@item.WavFile" /></a>
    

    由此(一个callAjax()函数被添加到onClick事件中)

    <a href="#" id="audiobtn" class="audiobtn" onclick="callAjax(@item.WavFile)"><img src="~/Content/audio.png" 
    title="Download" alt="@item.WavFile" /></a>
    

        $(".audiobtn").click(function (e) {
    
    $.ajax({
    type: "POST",
    url: "/EmployeeInfo/VoiceAuthorization",
    data: { audio: $(this).find('img').attr('alt') },
    async: false,
    cache: false,
    success: function (response) {
    

    function callAjax(param) {
    
    $.ajax({
    type: "POST",
    url: "/EmployeeInfo/VoiceAuthorization",
    data: { audio: param },
    async: false,
    cache: false,
    success: function (response) {