代码之家  ›  专栏  ›  技术社区  ›  Gregg B

将Jquery.load()与变量一起使用

  •  0
  • Gregg B  · 技术社区  · 14 年前

    $(document).ready(function() {
    
        origin = $('#origin').val();
        destination = $('#destination').val();
        weekDay = $('#weekday').val();
        fullSchedule = origin+'_'+destination+'_'+weekDay+'.html';
    
        $("#scheduleform :submit").click(function() {           
            $('#schedule').load(fullSchedule);          
            alert( fullSchedule );  
        });
    });
    

    我可以提醒完整的URL,但似乎无法将其加载到div中(id为schedule)。

    我基本上是在蹒跚学步,我知道什么小jquery做这个,所以我确定它是混乱的,但我只是不确定为什么它会正确地提醒,但不加载div。

    2 回复  |  直到 14 年前
        1
  •  5
  •   Vivin Paliath    14 年前

    如果你有Firebug,你能验证请求是否被发送到服务器并且得到了响应吗?也尝试回拨 load 要查看加载是否成功完成,请执行以下操作:

    $('#schedule').load(fullSchedule, function(responseText, textStatus, XMLHttpRequest) {
      alert("Load result: " + responseText + " " + textStatus);
    });
    

    其他一些事情:

    • URL是否有效?

    我注意到另一件事。你有什么理由 click() 而不是 submit() ? 所以:

    $("#scheduleform").submit(function() {
       ...
       return false; //You probably don't want the page to actually submit and reload
    });
    

    click 当然,我不知道你是否真的想提交任何东西。

        2
  •  1
  •   Gabriele Petrioli    14 年前

    您确定表单没有被提交并重新加载吗?您需要停止默认操作 preventDefault()

    $("#scheduleform :submit").click(function(e) {   
        e.preventDefault();        
        $('#schedule').load(fullSchedule);
    });