简体   繁体   English

如何使用jQuery的ajax数组发布JSON?

[英]how to use jquery ajax array post json?

i want to send array in ajax json post ,but some code are error.how to fig this code? 我想在ajax json post中发送数组,但是一些代码是错误的。

HTML 的HTML

 <table width="200" border="1">
      <tr>
        <td>table</td>
        <td>
          <input name="table[]" type="text" id="table[]" value="5" />
    </td>
      </tr>
      <tr>
        <td>menu</td>
        <td><input name="menu[]" type="text" id="menu[]" value="noodle" /></td>
      </tr>
      <tr>
        <td>number</td>
        <td><input name="number[]" type="text" id="number[]" value="1" /></td>
      </tr>
      <tr>
        <td>note</td>
        <td><input name="note[]" type="text" id="note[]" value="no " /></td>
      </tr>
      <tr>
        <td>table</td>
        <td><input name="table[]" type="text" id="table[]" value="1" /></td>
      </tr>
      <tr>
        <td>menu</td>
        <td><input name="menu[]" type="text" id="menu[]" value="beer" /></td>
      </tr>
      <tr>
        <td>number</td>
        <td><input name="number[]" type="text" id="number[]" value="2" /></td>
      </tr>
      <tr>
        <td>note</td>
        <td><input name="note[]" type="text" id="note[]" value="-" /></td>
      </tr>
    </table>
<button id="save-menu">save</button>
<button id="calculator">calculator</button>

Jquery : if I click button save-menu it send some data to menu/order or I click button calculator it send some data to calculator page. jQuery:如果单击按钮保存菜单,它将向菜单/命令发送一些数据,或者单击按钮计算器,它将向菜单页面发送一些数据。 But not my jquery code not send all value in array ,how to fig this code? 但不是我的jQuery代码未将所有值发送到数组中,如何计算此代码? Help me please.Thank you. 请帮助我。谢谢。

        <script>
        $(document).ready(function(){
$('#save-menu').click(function() {

        $.post('menu/order', {      
            table : $('select#workplace-table').val(),
            'mid[]' : $('input#order-mid').val(),
            'number[]' : $('input#order-number').val(),
            'note[]' : $('input#order-note').val(),
            }); 


   $('#calculator').click(function() {

        $.post('menu/calculator, {      
            table : $('select#workplace-table').val(),
            'mid[]' : $('input#order-mid').val(),
            'number[]' : $('input#order-number').val(),
            }); 


        });
        </script>

Don't use if(clickHandler) style syntax, that will never trigger when you want it to, instead just define the click handler: 不要使用if(clickHandler)样式的语法,该语法永远不会在您需要时触发,而只是定义click处理程序:

$('#save-menu').click(function() {
    //do stuff on save-menu click
})

It's the way you have the click events set up that are causing you your problems. 这是设置引发事件的单击事件的方式。 It should look more like this: 它看起来应该像这样:

$('#save-menu').click(function() {
  $.post(... ajax stuff...);
});

Bind an event handler to the "click" JavaScript event, or trigger that event on an element. 将事件处理程序绑定到“ click” JavaScript事件,或在元素上触发该事件。

$( "#save-menu" ).click(function() {
   //Handler for .click() called. 
   $.post('menu/order',{...});
});

$( "#calculator" ).click(function() {
   //Handler for .click() called. 
   $.post('menu/calculator',{...});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM