2012-08-06 33 views
0

是否有可能通过jQuery/js根据浏览器#tag更改表单动作的URL?表单动作的URL变化取决于#tag

这些标签正常工作,我只需要改变的行动。

这里是标签JS我目前使用的,我也使用jQuery插件地址也:

var QTABS = { 

init: function() { 
    // attached onload and change event to address plugin 
    $.address.init(function(event) { 
     // first load, set panel 
     QTABS.setPanel(event); 
    }).change(function(event) { 
     // if the url changes, set panel 
     QTABS.setPanel(event);   
    }); 
}, 

// the core function to display correct panel 
setPanel: function (event) { 

    // grab the hash tag from address plugin event 
    var hashtag = event.pathNames[0]; 
    // get the correct tab item, if no hashtag, get the first tab item 
    var tab = (hashtag) ? $('.tabs li a[href=#' + hashtag + ']') : $('.tabs li:first a'); 

    // reset everything to default 
    $('.tabs li').removeClass('activeTab'); 
    $('.tab_container .tab_content').hide(); 

    // if hashtag is found 
    if (hashtag) { 

     // set current tab item active and display correct panel 
     tab.parent().addClass('activeTab'); 
     $('.tab_container .tab_content:eq(' + (tab.parent().index()) + ')').show();   

    } else { 

     // set the first tab item and first panel    
     $('.tabs li:first').addClass('activeTab'); 
     $('.tab_container .tab_content:first').show();   

    } 

    if ($('.tabs').length) 
    { 
     // change the page title to current selected tab 
     document.title = tab.attr('title'); 
    } 
} 
} 

// Execute this script! 
QTABS.init(); 
+0

我可以看不出问题在哪里 - 如果你的代码有效,你可以轻松地将另一个函数添加到'init'和'change'函数中。在你的新函数中,使用包含hashtag的事件对象并根据需要设置表单动作,例如:$(“#form”)。attr('action',hashtag_or_something);' – WTK 2012-08-06 08:24:46

回答

0

这是我ahve成功使用

$("#myDelete").click(function(){ 
     $('#myForm').attr("action", "accounttrandelete.php"); 
     $("#myForm").submit(); 
     return false; 
    }); 
    $("#myEdit").click(function(){ 
     $('#myForm').attr("action", "accounttranedit.php"); 
     $("#myForm").submit(); 
     return false; 
    }); 
    $("#myRec").click(function(){ 
     $('#myForm').attr("action", "accounttranrec.php"); 
     $("#myForm").submit(); 
     return false; 
    }); 
    $("#myUnRec").click(function(){ 
     $('#myForm').attr("action", "accounttranunrec.php"); 
     $("#myForm").submit(); 
     return false; 
    }); 
0

因为我不知道在哪里的形式是和你想怎么访问它。如果你想改变动作URL要做到这一点:

$("form").attr("action", "Put your new Url here"); 

UPDATE

$("#tag")..attr("action", "Put your new Url here"); 

如果你想使用标准的JavaScript可以如下改变:

document.forms[0].action = "Put your new Url here"; 

注:您可以用您的表单名称替换forms[0]

UPDATE:

如果你有一个形式的ID,然后在其类似于上面的方法调用:

document.getElementById("tag").action = "Put your new Url here"; 
+0

感谢您的回复,I想用#tag追加到url – neoszion 2012-08-06 08:43:19

+0

@neoszion防止误解,你的意思是用'#tag'表示你的表单有id属性吗? – reporter 2012-08-06 09:08:36