dongyan7988 2015-03-07 04:29
浏览 102

使用jquery将多个输入框的值转换为多维数组

I have multiple input elements arranged in 16 rows and 5 columns (Just like a grid) . The contents of the 16 rows are inserted into a mysql table as 16 records with each record contains 5 fields. I want a method to get the value of all the input into an array using jquery and pass this to a ajax post request.

dataentry.php contains this code

jQuery('#metentry').submit(function(e){
     e.preventDefault(); 

 // getting data from form to variables
 var date=jQuery('#stddate').val();
 var kgsunit=jQuery('#unit').val();
 var kgsshift=jQuery('#shift').val();


//sending data to script
 jQuery.post("get_blank_form.php", {
 "date":date, 
 'unit':kgsunit,
 'shift':kgsshift, 
 },    function(data) {      

  //displaying message
jQuery('#blankform').html(data);
jQuery('#formpanel').hide();


  });

get_blank_form.php contains this code:

echo'<form id="shiftentry" name="shiftentry" >';


 echo "Date:<input id=shiftdate value='".$date."'/>"; 
 echo "Unit:<input id=shiftdate value='".$unit."'/>";
 echo "Shift:<input id=shiftdate value='".$shift."'/><br/>";

   echo "<table><tr><th>No</th><th>Ele</th><th>Location</th><th>Drate </th><th>H3 Val </th><th>Part </th>   <th>Iod</th><th>Cont. </th></tr>";
            while($row2=mysql_fetch_array($result_sec))
                { 
                echo "<tr>";
                echo "<td>".++$rc."</td>";
                echo "<td><input size='5' id='".$row2['ele']."' value='".$row2['ele']."' /></td>";
                echo "<td><input id='".$row2['loc_id']."' value='".$row2['loc']."' /></td>";
                echo "<td><input size='5' id='drate".$rc."'  value='".$row2['drate']."'/></td>";
                echo "<td><input size='5' id='h3".$rc."' value='0' /></td>";
                echo "<td><input size='5' id='part".$rc."' value='0' /></td>";
                echo "<td><input size='5' id='iod".$rc."' value='0' /></td>";
                echo "<td><input size='5' id='cont".$rc."' value='0' /></td>";
                echo "</tr>";
            }



  echo " </table>";



   echo '<div align="center">';
   echo '<input type="submit" id="submit2" name="submit2" value="Submit" width="30" />';
   echo '</div>';

 echo '</form>';

Both the above scripts are working and this will add a form"shiftentry" to the DOM in dataentry.php. This form conains around 21 rows and 8 columns containing input elements. I want a method to get the values in this 21 x 8 input elements and pass it to a jQuery post request. There are two issues.

  1. Since the form is added after the DOM is loaded, how to get the value?
  2. Is there any way to read all the input values using a loop instead of separate lines for each input element?

My final aim is to append this 21 rows into a mysql table. This part I knew, once I get all the values. Any suggestions ?

I have this function which create a multi dimensional array of input values. But in this casethe 'form1' is loaded before DOM is completed. But in the above scenario, form is dynamically added after DOM loding is completed

jQuery('#form1').submit(function(e){
     e.preventDefault(); 

        var arr = jQuery('#form1 tr:gt(0)').map(function() {
    return [jQuery('input', this).map(function() {
        return this.value;
    }).get()];
}).get();


});

I also have another jquery script in this page which is used to traverse through the input boxes (obviously, these input boxes are loaded after DOM)

jQuery(document).delegate('input','keyup',function(e){
if(e.which==39)
jQuery(this).closest('td').next().find('input').focus();
else if(e.which==37)
jQuery(this).closest('td').prev().find('input').focus();
else if(e.which==40)
jQuery(this).closest('tr').next().find('td:eq('+jQuery(this).closest('td').index()+')').find('input').focus();
else if(e.which==38)
jQuery(this).closest('tr').prev().find('td:eq('+jQuery(this).closest('td').index()+')').find('input').focus();
});

How can I use the delegate option in the script used for traverse in the script for getting input values?

  • 写回答

1条回答 默认 最新

  • dqrfdl5708 2015-03-07 08:06
    关注

    On form submit we can use $("#frmServiceMaster").serialize() to get the serialize string if you want JSON then use $("#frmServiceMaster").serializeArray() It will convert all form input to String or JSON

    $("#frmServiceMaster").submit(function(e){
    event.preventDefault();
    alert($("#frmServiceMaster").serialize());
        alert($("#frmServiceMaster").serializeArray());
    
    });
    

    working FIDDLE

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。