簡體   English   中英

在表單提交時發送元素 ID

[英]Sending element id's upon form submit

我需要獲取從丟棄的項目(使用 jqueryui draggables/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 是這樣的(不確定它是否正常工作):

  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 添加到發送到服務器的數據中,您可以使用擴展或將它們添加到 Ajax function 中。

延長:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM