由于有旧版代码,我有一个带有一系列复选框的链接,当用户单击某个复选框时,其值将作为逗号分隔列表附加到href上。
麻烦在于,既然我正在更新代码,我发现对href的跟踪要比对href的调整快,因此该列表被排除在外。
我尝试使用很棒的preventDefault()
,但是我不知道如何在将href更改为包括所选值后如何继续默认操作。
我还要指出,将其更改为常规格式是不可行的,因为提交后,灯箱中还有一个附加选项。 (不要问我为什么,这是我书中的疯子)
到目前为止,我必须
$(function(){
$('#cnt-area form table tbody').dragCheck();
// I just split up the .each and .click to see if it mattered, which it doesnt
$('.multi_assign_link').each(function(){
$(this).click(function(e){
e.preventDefault();
var href = $(this).attr('href');
$('#cnt-area form input:checkbox').each(function(){
if($(this).attr('checked')){
if(href.search(','+$(this).val()) == -1){
href += ','+$(this).val();
}
}else{
var s = ','+$(this).val();
s.toString();
href = href.replace(s, '');
}
});
$(this).attr('href',href);
// Continue to follow the link
// Faking a user click here with $(this).click(); obviously throws a loop!
});
});
});
最佳答案
只需将位置设置为调整后的href值,然后从处理程序中返回false即可避免获取原始链接。
$(function(){
$('#cnt-area form table tbody').dragCheck();
// I just split up the .each and .click to see if it mattered, which it doesnt
$('.multi_assign_link').each(function(){
$(this).click(function(e){
var href = $(this).attr('href');
$('#cnt-area form input:checkbox').each(function(){
if($(this).attr('checked')){
if(href.search(','+$(this).val()) == -1){
href += ','+$(this).val();
}
}else{
var s = ','+$(this).val();
s.toString();
href = href.replace(s, '');
}
});
location.href = href;
return false;
});
});
});
关于javascript - jQuery〜在遵循它之前先调整href,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3451719/