.load开始新路径 [英] .load start new path

查看:110
本文介绍了.load开始新路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目我尝试一个模式引导添加到我的观点1,我用 .load()加载观点,就是当我尝试加载它只会出现黑色的观点,我试图用的console.log()来观看错误,我看到这个

的http://本地主机:38212 / Desafios /细节/ Solucoes /细节ID = 14

当我点击div来触发模式,我在 / Desafios /详细信息,我想去 / Solucoes /详情,而不是所有的网址,我不想在 .load() URL添加到previous网址,我不得不。我怎样才能做到这一点?

下面是我的jQuery函数

  $(#VisualizarSolucao)。点击(函数(事件){
    VAR ID = $(本).attr(数据ID);
    $(#模式)。负载(../ Solucoes /详细信息?ID =+ ID,函数(){
        $(#模)模态()。
    })
    事件。preventDefault()
});

我加载JS的一个外部文件,这里是呼吁单击事件VisualizarSolucao ID

 < D​​IV CLASS =COL-MD-10的风格=BORDER-左:2px的纯黑色>
    < p风格=文本对齐:左;填充顶:5像素>
        < A HREF =ID =VisualizarSolucao数据ID =@ i.SolucaoId的风格=FONT-重量:900> @ i.SolucaoTitulo< / A>
    &所述; / P>
    < BR />
    < D​​IV CLASS =COL-MD-偏移4 COL-MD-8的风格=文本对齐:右>
        <文字和GT;克里亚POR< A HREF =#> @ i.User.UserName< / A>没有直径@ i.DataCriacao< /文字和GT;
    < / DIV>
< / DIV>


解决方案

.load的网址()的功能将需要 / Solucoes /详细信息?ID =+ ID ,但是你应该始终使用 @ Url.Action()方法,以确保您的网址作为正确产生的。

由于该脚本是在外部文件中,并剃须刀code未在外部文件解析,HTML改变你

 < A HREF =#类=VisualizarSolucao数据URL =@ Url.Action(详细信息,Solucoes,新{ID = i.SolucaoId })>

请注意, ID 更改为名(您生成重复 ID 属性在一个循环是无效的HTML)。你也应该删除内嵌样式并使用CSS来代替。

然后,更改脚本(无需事件。preventDefault()

  $('。VisualizarSolucao')。点击(函数(){//类名选择
    VAR URL = $(本)。数据(URL); //使用数据(),不AT​​TR()
    $(#模式)。负载(URL,函数(){
        $(#模)模态()。
    })
});

in my project im trying to add a modal bootstrap to 1 of my views and i use .load() to load that view, the thing is when i try to load the view it only appears black, i tried to use console.log() to watch the errors and i saw this

http://localhost:38212/Desafios/Details/Solucoes/Details?id=14

when i click the div to trigger the modal, i am in /Desafios/Details, and I want to go to /Solucoes/Details and not all the url, I don't want to add the .load() url to the previous url that i had. How can i do that?

Here is my jquery function

$("#VisualizarSolucao").click(function (event) {
    var id = $(this).attr("data-id");
    $("#modal").load("../Solucoes/Details?id=" + id, function () {
        $("#modal").modal();
    })
    event.preventDefault()
});

i am loading the js in a external file, here is the VisualizarSolucao id that is called on the click event

<div class="col-md-10" style="border-left:2px solid black">
    <p style="text-align:left;padding-top:5px">
        <a href="" id="VisualizarSolucao" data-id="@i.SolucaoId" style="font-weight:900">@i.SolucaoTitulo</a>
    </p>
    <br />
    <div class="col-md-offset-4 col-md-8" style="text-align:right">
        <text>Criado por <a href="#">@i.User.UserName</a> no dia @i.DataCriacao</text>
    </div>
</div>

解决方案

The url in the .load() function would need to be "/Solucoes/Details?id=" + id, however you should always use the @Url.Action() method to ensure your url's as correctly generated.

Since the script is in an external file, and razor code is not parsed in external files, change you html to

<a href="#" class="VisualizarSolucao" data-url="@Url.Action("Details", "Solucoes", new { id = i.SolucaoId })">

Note that the id is changed to a class name (your generating duplicate id attributes in a loop which is invalid html). You should also remove the inline styles and use css instead.

Then change the script to (no need for event.preventDefault())

$('.VisualizarSolucao').click(function() { // class name selector
    var url = $(this).data('url'); // use data(), not attr()
    $("#modal").load(url, function () {
        $("#modal").modal();
    })
});

这篇关于.load开始新路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆