简体   繁体   中英

Grab Data on the Fly from Table HTML

This might be a little difficult to explain, but I am displaying a grid that is 2 x 2. When you click on the text of a cell (say 0,1) it will bring up a pop-up box and store those coordinates in hidden values. Those hidden values will be then submitted to a form via POST.

Here is some sample code I have written to hopefully clarify what I am trying to do: https://jsfiddle.net/v08bzm9k/1/

<table border=1>
<tr>
  <td class='grid-cells'><a href='#myPopup' data-rel='popup'>1</a></td>
  <td class='grid-cells'><a href='#myPopup' data-rel='popup'>2</a></td>
</tr>
<tr>
  <td class='grid-cells'><a href='#myPopup' data-rel='popup'>3</a></td>
  <td class='grid-cells'><a href='#myPopup' data-rel='popup'>4</a></td>
</tr>
</table>

<div data-role="popup" id="myPopup" class="ui-content" style="min-width:250px;">
      <form method="post" action="save-square.php">
        <div>
          <h3>Pick This Square:</h3>
          <label for="name" class="ui-hidden-accessible">Name:</label>
          <input type="text" name="name" id="name" placeholder="Name">
          <label for="email" class="ui-hidden-accessible">Email:</label>
          <input type="text" name="email" id="email" placeholder="Email">
          <input type="submit" data-inline="true" value="Submit">
          <div id='row'></div>
          <div id='col'></div>
        </div>
      </form>
    </div>

Would I store the values of the cell I click into hidden fields via JavaScript?

I updated https://jsfiddle.net/v08bzm9k/2/

Yes you can use hidden fields. In your form add this:

<input type="hidden" name="coords" id="coords" value="">

Add this JS to your page:

function setCoords(newCoords) {
    $('#coords').val(newCoords);
}

And in your links, add an onClick:

<a href='#myPopup' data-rel='popup' onclick="setCoords('1,1');">1</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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