2015-07-20 45 views
-1

我有一个URL如何在onClick时将自定义href设置为按钮?

http://localhost:8080/BIM/teacher/reports/section-exercise/assignment?x=1&y=2&z=3

我有2个btns

enter image description here

我不知道我怎么会绑定的2 BTN正确的URL。

如果点击Remediation,我想将href设置为

http://localhost:8080/BIM/teacher/reports/section-exercise/remediation?x=1&y=2&z=3

,唯一不同的是,第4段,其余部分保持不变。

任何对他们的帮助/建议将非常感谢!

我猜 - 我可能要:

  • 抢整个URL
  • 重新构建的newURL,它存储在一个变量
  • 绑定是的newURL我的BTN。

我希望我在这条正确的道路上。 让我知道,如果你们,注意到一些事情。

+0

ngHref对于构建/修改href字符串很有用。 –

+0

我在其他人项目上工作,我允许使用的唯一框架是jQuery。我希望Angular允许。 – ihue

+0

对不起,由于某种原因,Stackoverflow将这个弹出到我的AngularJS feed中。你的意思是 –

回答

1

在这里,您可以将网址更改为任何您喜欢的。 我使用更换温控功能,以动态更改网址

var url='http://localhost:8080/BIM/teacher/reports/section-exercise/assignment?x=1&y=2&z=3' 

$('button1').click(function() { 
    $(this).attr('href', url); 
}); 
$('button2').click(function() { 
    $(this).attr('href', url.replace('assignment','remediation'); 
}); 
+0

您的建议是否容易扩展?只是好奇。 – ihue

+0

当然。你想如何扩大? – Aminadav

+0

我想这对我来说已经足够了。感谢帮助。奖励是你的。 – ihue

1
$(/*remediation button selector*/).click(function() { 
    $(/*assignment performance button selector*/).attr('href', 'http://localhost:8080/BIM/teacher/reports/section-exercise/remediation?x=1&y=2&z=3'); 
}); 

我不知道,如果你的意思是你想采取补救措施的按钮来更改HREF第一个按钮,或者如果你想他们只是有不同的HREFs;如果是后者,他们可以直接在HTML中拥有不同的hrefs,不需要JS。

+0

'?'后面的部分将会更改。我不确定我是否应该按照自己的方式去做。 – ihue

+0

请更正我 - 如果我错了 – ihue

1

Aminas回答是好,但如果第一URL并不总是我会推荐这个功能(使用正则表达式),只有改变同文件的URL的名称:

function changeFileName(strURL, strNewName) { 
    return strURL.replace(/[^\/?]*(?=\?|$)/, strNewName); 
} 

本(一)的作品不管是什么原文件的名字,和(b)如果路径中包含文件名不会造成问题。

相关问题