bla*_*d Ψ 27 jquery response.redirect dynamic-data
我有一个脚本如下
$('.button1').click(function() {
document.location.href=$(this).attr('id');
});
Run Code Online (Sandbox Code Playgroud)
button1具有可变唯一ID.单击时,页面必须重定向到URL" www.example.com/index.php?id=buttonid
",但现在页面仅重定向到" button id
".
我想www.example.com/index.php?id=
在当前网址之前添加字符串" ".我怎样才能做到这一点?
Tom*_* Tu 64
$('.button1').click(function() {
window.location = "www.example.com/index.php?id=" + this.id;
});
Run Code Online (Sandbox Code Playgroud)
首先使用window.location
是更好的,因为根据规范document.location
值是只读的,可能会导致你在旧的/不同的浏览器中头痛.检查笔记@ MDC DOM document.location页面
而对于第二种 - 使用attr
jQuery方法获取id是一种不好的做法 - 你应该使用直接本机DOM访问器this.id
作为分配给this
普通DOM元素的值.
您需要指定域:
$('.button1').click(function() {
window.location = 'www.example.com/index.php?id=' + this.id;
});
Run Code Online (Sandbox Code Playgroud)
$('.button1').click(function() {
document.location.href='/index.php?id=' + $(this).attr('id');
});
Run Code Online (Sandbox Code Playgroud)