【问题标题】:Dynamically Modify $.post url动态修改 $.post url
【发布时间】:2013-04-29 16:44:01
【问题描述】:

$.post 可以镜像表单的action='' 吗?

我不想创建多个$.post 函数,但是action='' 会随着用户在菜单系统中选择他/她想要提交的内容而改变。所以,如果 url 随 action='' 动态变化,我只需要一个 $.post 函数。

$("#txtrform").submit(function(){

    $.post('{*ACTION*#txtrform}', $("#txtrform").serialize(), function(data) {
        $("#col3").load("/include/txtrpbox/feed.php");
        $('input#txtrinput').val('');
    });

    return false;       
});

【问题讨论】:

  • 你可以使用jQuery的.attr()方法。 $("#txtrform").attr('action');

标签: jquery ajax forms dynamic


【解决方案1】:
$("#txtrform").submit(function(){

    $.post($(this).attr('action'), $(this).serialize(), function(data) {
      ...
    });

    return false;       
});

【讨论】:

  • 谢谢!!!!!!!!!!!!!!!这是第三个主题,你回答得最简单!我想这就是我问这个问题的方式。
【解决方案2】:

将其分解为变量

var Target;
$("#txtrform").submit(function(){

    $.post(Target, $(this).serialize(), function(data) {
        $("#col3").load('/include/txtrpbox/feed.php');
        $('input#txtrinput').val('');
    });

    return false;       
});

Target = '/include/1.php';

//submit now will go to 1.php

Target = '/include/2.php';
//submit now will go to 1.php

【讨论】:

  • 不错。但对于我的平台,第一个答案更简单,因为我有太多目标。
猜你喜欢
  • 2012-12-12
  • 2013-10-27
  • 2019-03-15
  • 2013-05-19
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 2021-11-10
  • 2014-07-16
相关资源
最近更新 更多