最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Displaying form data to a table dynamically - Stack Overflow

programmeradmin2浏览0评论

Here is My JsFiddle

After Filling the form and clicking add button, i want the form data to be displayed inside the table. can someone help me how can i do it with jquery.

<div class="form-div">
<form class="form-inline">
    <fieldset>
        <legend>Item Details</legend>
        <label>Enter Item Name</label>
        <input type="text" placeholder="Item Name">
        <label>Quantity</label>
        <select class="input-mini">
          <option>1</option>
          <option>2</option>
          <option>3</option>
          <option>4</option>
          <option>5</option>
        </select>
        <button class="btn">Add Data</button>
    </fieldset>
</form>
</div>
<div class="table-div">
<table class="table">
    <thead>
        <tr>
            <th> S.no </th>
            <th> Item Name </th>
            <th> Quantity </th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
</div>

Here is My JsFiddle

After Filling the form and clicking add button, i want the form data to be displayed inside the table. can someone help me how can i do it with jquery.

<div class="form-div">
<form class="form-inline">
    <fieldset>
        <legend>Item Details</legend>
        <label>Enter Item Name</label>
        <input type="text" placeholder="Item Name">
        <label>Quantity</label>
        <select class="input-mini">
          <option>1</option>
          <option>2</option>
          <option>3</option>
          <option>4</option>
          <option>5</option>
        </select>
        <button class="btn">Add Data</button>
    </fieldset>
</form>
</div>
<div class="table-div">
<table class="table">
    <thead>
        <tr>
            <th> S.no </th>
            <th> Item Name </th>
            <th> Quantity </th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
</div>
Share Improve this question asked Oct 4, 2013 at 17:01 user2847429user2847429 3
  • Have you tried anything out yet? I don't see any jQuery in your fiddle either. – Chris Rockwell Commented Oct 4, 2013 at 17:07
  • Actually i don't know how to get form data. i just know doing $('.table > tbody:last').append('<tr>...</tr>'); would append the data. but i am not sure what to write inside – user2847429 Commented Oct 4, 2013 at 17:11
  • Rajesh has a great answer - study it until you understand every line. (The purpose of the return false is to stop the button from submitting the form). Tiago Alves also has a good point: use IDs to address (select) each element specifically. – cssyphus Commented Oct 4, 2013 at 17:15
Add a ment  | 

3 Answers 3

Reset to default 4

You should give unique identifiers to the elements you want to manipulate. After that you can do something like this using click(), val() and append() jQuery functions:

var sno = 0;
$("form button.btn").click(function() {
    var item_name = $("#item_name").val();
    var quantity = $("#quantity").val();
    var new_row = "<tr><td>" + ++sno + "</td><td>" + item_name + "</td><td>" + quantity + "</td></tr>";
    $("table tbody").append(new_row);
    return false;
});

Working demo

Better give a ID to form and table elements if they are not unique in the window and also to optimize DOM selections.

Wrote the code and added fiddle.

You need to get values of text and select and create a new row, then add to tbody.

Fiddle: http://jsfiddle/aH6hb/2/

try this

<div class="">

        <fieldset>
            <legend>Item Details</legend>
            <label>Enter Item Name</label>
            <input type="text" placeholder="Item Name" id="item_name">
            <label>Quantity</label>
            <select class="input-mini" id="quentity">
              <option>1</option>
              <option>2</option>
              <option>3</option>
              <option>4</option>
              <option>5</option>
            </select>
            <button class="btn" onclick="viewRecord()">Add Data</button>
        </fieldset>

</div>
<div class="">
    <table class="table controls controls-row">
        <thead>
            <tr id="heading">
                <th> S.no </th>
                <th> Item Name </th>
                <th> Quantity </th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>

js part

var index=0;
function viewRecord(){
    index++;
$('table tbody').append('<tr><td>'+index+'</td><td>'+$("#item_name").val()+' </td><td> '+$("#quentity option:selected").val()+'</td></tr>');
}

Working Demo

发布评论

评论列表(0)

  1. 暂无评论