表单操作网址根据#tag更改

时间:2012-08-06 08:16:53

标签: javascript jquery

是否可以使用jQuery / js更改表单操作Url,具体取决于浏览器#tag?

标签工作正常我只需更改动作。

这是我目前使用的标签js,我也使用jQuery Address Plugin:

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();

2 个答案:

答案 0 :(得分:0)

因为我不知道表单的位置以及您希望如何访问它。如果您想更改操作网址,请执行以下操作:

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

<强>更新

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

如果您想使用标准Javascript,可以按以下方式更改:

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

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

更新:

如果你有一个带有id的表单,那么它类似于上面的方法调用:

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

答案 1 :(得分:0)

这是我成功使用的

  $("#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;
    });
相关问题