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

javascript - JQuery get select element inside table - Stack Overflow

programmeradmin2浏览0评论

I have the following table element which has a select element in its td. The table id (table1 in the example) can keep changing. So, how can I fetch the selected option using table ID in JQuery?

    <table class="indexViews" id = "table1">
   <tr>
    <td align="center", width="10%" valign="middle"></td>
    <td align="center", width="70%" valign="middle">
        <select  id="indexViewsList" class="indexViewsListWnd" onChange="switchIndexView(this)">
            <option id="0" value="0" selected="selected">Index Quick View</option>
            <option id="1" value="1">Identifier View</option>
            <option id="2" value="2">Commodity</option>
        </select></td>
    <td align="right", width="10%" valign="middle"><input class="searchIcon" type="image" src="../../images/downloadIcon.gif" alt="Submit" onClick="downloadIndexFile()"></input></td>
   </tr>
</table>

I have the following table element which has a select element in its td. The table id (table1 in the example) can keep changing. So, how can I fetch the selected option using table ID in JQuery?

    <table class="indexViews" id = "table1">
   <tr>
    <td align="center", width="10%" valign="middle"></td>
    <td align="center", width="70%" valign="middle">
        <select  id="indexViewsList" class="indexViewsListWnd" onChange="switchIndexView(this)">
            <option id="0" value="0" selected="selected">Index Quick View</option>
            <option id="1" value="1">Identifier View</option>
            <option id="2" value="2">Commodity</option>
        </select></td>
    <td align="right", width="10%" valign="middle"><input class="searchIcon" type="image" src="../../images/downloadIcon.gif" alt="Submit" onClick="downloadIndexFile()"></input></td>
   </tr>
</table>
Share Improve this question asked Oct 7, 2013 at 19:12 DineshDinesh 734 silver badges10 bronze badges 3
  • 2 Do both ids keep changing, or just the table element's? What makes them change (and honestly, changing an id doesn't make a lot of sense, really)? – David Thomas Commented Oct 7, 2013 at 19:14
  • 3 First, why does the table id keep changing, and second does the table ID HAVE to keep changing? – kevindeleon Commented Oct 7, 2013 at 19:14
  • Only the table ID keeps changing. This is because I have multiple windows which have this table. I append this table element to each window while creating them. I assign a unique table ID to each window while appending. – Dinesh Commented Oct 7, 2013 at 19:17
Add a ment  | 

4 Answers 4

Reset to default 2

I don't understand why your id keep changing. But maybe you will want to iterate through your table using jquery selector on class name.

var lstIndexView = $(".indexViews .indexViewsListWnd");

http://api.jquery./class-selector/

try below code

var selectedValue= $(".indexViews .indexViewsListWnd").val();

You can use this selector in jQuery $('table[id^="table"]')

Jquery - "Attribute starts with" selector

This is how you get it using table id.

$('#table1 #indexViewsList').val();

But better you get this select directly:

$('#indexViewsList').val();
发布评论

评论列表(0)

  1. 暂无评论