Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 提交表单时发送元素id_Javascript_Jquery - Fatal编程技术网

Javascript 提交表单时发送元素id

Javascript 提交表单时发送元素id,javascript,jquery,Javascript,Jquery,我需要使用jqueryui Dragables/droppables/sortables从拖放项目中提取的元素id获取数据,然后通过表单发送,并在此处发送数据: function submitForm() { $.ajax({type:'POST', url: 'send_tile.php', data:$('#tiles_collected').serialize(), success: function(response) {

我需要使用jqueryui Dragables/droppables/sortables从拖放项目中提取的元素id获取数据,然后通过表单发送,并在此处发送数据:

      function submitForm() {
$.ajax({type:'POST',
       url: 'send_tile.php',
       data:$('#tiles_collected').serialize(), 
       success: function(response) {
    $('#tiles_collected').find('.form_result').html(response);
}});

return false;
   }
获取ID的功能不确定是否正常工作:

  function getSku() {
var myIds = new array();
        $("#collection li").each(function(index, element){
              alert($(this).attr('id'));
           myIds.push(element.id);

            });
表格如下所示:

     <form id="tiles_collected" name='tiles_collected' method="post" onsubmit="return submitForm();">
      Email:<input name='email' id='email' type='text'/><br />
      Name:<input name='name' id='name' type='text' /><br />
       Zip code: <input name='zip' id='zip' type='text' /><br />
       <input type='hidden' id='sku' />
       <input type="submit" name="submit" value="Email collection to yourself and us" /><br/>
       <div class="form_result"></div>
       </form>

有人能帮我一下吗?

要将ID添加到发送到服务器的数据中,可以使用extend,也可以在Ajax函数中添加ID

扩展:

function submitForm() {
    var MyData = $('#tiles_collected').serialize();
    $.extend(MyData, {IDs: myIds});  //myIds will have to be accessable, right now it's a local variable in your getSku() function

    $.ajax({type:'POST',
       url: 'send_tile.php',
       data: MyData, 
       success: function(response) {
            $('#tiles_collected').find('.form_result').html(response);
       }
    });
    return false;
}
或者直接添加它们:

function getSku() {
    var myIds = new array();
    $("#collection li").each(function(index, element){
        myIds.push(element.id);
    });
    return myIds;
}

var IDs = getSku();

$.ajax({type:'POST',
    url: 'send_tile.php',
    data: {data : $('#tiles_collected').serialize(), ids : IDs},
    success: function(response) {
        $('#tiles_collected').find('.form_result').html(response);
    }
});

在send_tile.php中的第二个示例中,我是否将其称为“ids”以实际获取要打印到电子邮件中的值。是的,在php中,ajax函数中发送的值在$u POST['ids'中提供等等。只要做一个print\u r$\u POST,看看它保存了什么或变量转储等。$'tiles\u collected'。serialize似乎不再使用该方法工作,我仍然看不到任何'id',您必须更改服务器端脚本才能工作,因为序列化的表单现在将位于$\u POST['data']变量中,而不仅仅是$\u POST,并且ID将在$u POST['ID']中。在我看来,这是一种方法,将数据发送到服务器,以便在$\u POST['something']中可以访问,但随后您必须更改服务器端代码以捕获这些值。在修复php文件后,我得到了要显示的表单值,thx。但是我没能让身份证显示出来。我想这是因为我的getSku函数没有显示它应该显示的ID。我想是坏功能吧。