I know that a code like this is used for sending data to php file without refreshing by using ajax, but this code only works for one form with some inputs, my problem is: I have a web page that in it there are 5 buttons and each button is related to different input, I want to send data with ajax without refreshing,for example when I click on button 1, it sends its own inputs and when I click button n it sends its own data, how can I do that?

我知道这样的代码是用于将数据发送到php文件没有通过使用ajax刷新,但是这段代码只适用于一种形式与一些输入,我的问题是:我有一个网页,它有5个按钮,每个按钮不同的输入有关,我想用ajax发送数据没有刷新,例如当我点击按钮1,它发送自己的输入和当我点击按钮n它发送自己的数据,我可以怎么做呢?

<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
      $(function () {

    $('form').on('submit', function (e) {

      e.preventDefault();

      $.ajax({
        type: 'post',
        url: 'led.php',
        data: $('form').serialize(),
        success: function () {
        }
      });

    });

  });
   </script>
 </head>
 <body>
    <form>
      <input type='hidden' name="key" value="on">
      <input name="key" type="submit" value="ON">
    </form>
  </body>
</html>

6 个解决方案

#1


2

" but this code only works for one form with some inputs "

"但是这个代码只适用于有输入的表单"

Technically speaking, that will work with any form on the page. (as you are targeting the form tag)

从技术上讲,这将适用于页面上的任何形式。(因为您的目标是表单标记)

If you want to target mutliple buttons and process them the same way add a class to them.

如果您想要针对mutliple按钮,并以相同的方式处理它们,则向它们添加一个类。

If you want to target seperate elements individually, add an id.

如果您想要单独地针对分离元素,请添加一个id。

You then target classes as $('.classname') and ids as $('#id') notice the . and # (as with css selectors)

然后将类目标设置为$('.classname'), id设置为$('#id')。和#(与css选择器一样)

SEE LIVE EXAMPLE here

看到生活的例子

$('.submit_to_a').parent('form').on('submit', function(e) {

  e.preventDefault();

  $.ajax({
    type: 'post',
    url: 'a.php',
    data: $(this).serialize(),
    success: function() {}
  });

});


$('#gotob').parent('form').on('submit', function(e) {

  e.preventDefault();

  $.ajax({
    type: 'post',
    url: 'b.php',
    data: $(this).serialize(),
    success: function() {}
  });

});

$('#gotoc').parent('form').on('submit', function(e) {

  e.preventDefault();

  $.ajax({
    type: 'post',
    url: 'c.php',
    data: $(this).serialize(),
    success: function() {}
  });

});
<form action="a.php">
  <input type="text">
  <input type="text">
  <input type="text">
  <button type="submit" class="submit_to_a">goes to a</button>
</form>
<form action="a.php">
  <input type="text">
  <input type="text">
  <input type="text">
  <button type="submit" class="submit_to_a">goes to a</button>
</form>
<form action="b.php">
  <input type="text">
  <input type="text">
  <input type="text">
  <button type="submit" id="gotob">goes to b</button>
</form>
<form action="c.php">
  <input type="text">
  <input type="text">
  <input type="text">
  <button type="submit" id="gotoc">goes to c</button>
</form>

更多相关文章

  1. [php]在PHP中读取和写入WORD文档的代码
  2. php评论回复无限极嵌套如何实现?已写基本代码,期待高手解惑
  3. advertising.php源代码分析
  4. thinkphp友情链接代码
  5. 如何在php中找到更轻或更暗版本的十六进制代码的十六进制代码
  6. PHP 网站保存快捷方式的实现代码
  7. 正则表达式匹配wordpress类似的短代码,用于自闭和封闭。
  8. php文件显示代码在Chrome
  9. 怎么知道php代码运行时调用了那个类、那个方法呢?

随机推荐

  1. Android屏幕分辨率
  2. Android按键消息传播流程(WindowManagerSe
  3. android 笔记 --- Android中Menu应用
  4. OrmLite - Lightweight Java ORM Support
  5. TextView的跑马灯效果
  6. Android 4.0 用户输入子系统
  7. Activity 模版样式简介
  8. Android中ExpandableListView的使用
  9. android 对话框(Dialog)使用
  10. Android Sensor Shake(WeChat)