I have 5 columns in my jQuery data table and the fifth one is a dropdown list as shown below:
I want to save the data in this data table. So I want to iterate through all the columns of all rows in this data table. The fifth column is a drop down list and the user selects one item from the list. The item’s name is displayed in the dropdown list but each dropdown item has an id value and a text name in it. And "None" is the default value shown in the drop down list at first. The DataSet is used to populate the data table and Dropdown list is also populated with another dataset.
In .cshtml
<tbody>
@foreach (System.Data.DataRow row in DataSet.Tables[0].Rows)
{
<tr>
@foreach (System.Data.DataColumn column in DataSet.Tables[0].Columns)
{
<td>@row[column]</td>
}
<td>
<div class="ddlPanel">
<select id="ddlSettingName" name="settingNameList" class="form-control select2">
<option value="0">None</option>
@foreach (var item in ViewBag.DropdownOptions) // viewbag is dataset
{
<option value="@item.Value">@item.Text</option>
}
</select>
</div>
</td>
</tr>
}
</tbody>
And the code for getting each column values of each row including the id and text value of the dropdown is shown below:
<script type="text/javascript">
function Save() {
var newDoorListForAdd = new Array();
var table = document.getElementById("newTable");
var tableLength = table.rows.length;
for (var i = 0; i < tableLength - 1; i++) {
var trial = $("#ddlSettingName option:selected").text();
alert(trial);
}
}
The above code is not working. I want to get all the values (including id and text value of dropdown selected item) of each row. How can I achieve it ?
2
Answers
use something like this:
You can use
class="ddlSettingName"
instead ofid="ddlSettingName"
.I write an example use your code and I hope this help you.
the result is: