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

javascript - From a table row with TWO dropdowns, get selected value & which drop-down changed - Stack Overflow

programmeradmin0浏览0评论

I am trying to update two cascading drop down inside a table there are many answers on SO for drop downs, but I was unable to find help on cascading updates inside a Table with dynamically added rows.

Given the many rows they all have varying Id's filter #Id does'nt work. So, How do I identify which rows Dropdown triggered the change & cascade the update another Dropdown/cell in the next col of the same row?

There are 2 DropDownList (select box) inside a table row cell. To simplify this, the first is Country -> Second is state. So a user is expected to select the country and then the state.


My pseudo algorithm:

  • find which one was fired, and which row (unsure if its needed, should I wire in place)
  • then fire an ajax call... to get the values based on country
  • update the second drop down with value in the same table row.
  • Case 1 Change country and then change state.
  • Case 2 Just change State, but first get the Country value in the first dropdown of the same row.

I know how to get the change and value in a regular page, but how do I get those changes and update the adjacent dropdown.

$(document).on('change', 'select', function(){
      var data = $(this).val();
      alert(data);
});

Edit, Stephen request to show HTML

<tr>
  <td>
    //DropDown 1 (Imagine Country)
    <span class="projectcodeid"> 
      <select class="form-control" id="Records_1__TCode_Project_ID" name="Records[1].TCode.Project.ID"><option value=""></option>
       <option value="1">Plumbing</option>
       <option value="2">Modeling</option>
      </select></span>                                                    
  </td>

  <td>
    //DropDown 2 (Imagine State)
    <input type="hidden" name="Records.Index" value="1">
    <input class="timecodeid" name="Records[1].TCode.ID" type="hidden" value="5">                                                    
    <span class="timecode timecodeDdlId"> <select class="form-control timecodeDdlId" id="Records_1__TCode_ID" name="Records[1].TCode.ID"><option value=""></option>
</select></span>
    </td>

    <td>
        <input name="Records[1].DataRecords[0].ID" type="hidden" value="">
        <input class="hours" name="Records[1].DataRecords[0].Work" type="text" value="">
    </td>                                                

    <td>
       <input class="bs-checkbox" name="Records[1].DeleteRow" type="checkbox" value="true"><input name="Records[1].DeleteRow" type="hidden" value="false">
    </td>
 </tr>

Sample image for clarification

I am trying to update two cascading drop down inside a table there are many answers on SO for drop downs, but I was unable to find help on cascading updates inside a Table with dynamically added rows.

Given the many rows they all have varying Id's filter #Id does'nt work. So, How do I identify which rows Dropdown triggered the change & cascade the update another Dropdown/cell in the next col of the same row?

There are 2 DropDownList (select box) inside a table row cell. To simplify this, the first is Country -> Second is state. So a user is expected to select the country and then the state.


My pseudo algorithm:

  • find which one was fired, and which row (unsure if its needed, should I wire in place)
  • then fire an ajax call... to get the values based on country
  • update the second drop down with value in the same table row.
  • Case 1 Change country and then change state.
  • Case 2 Just change State, but first get the Country value in the first dropdown of the same row.

I know how to get the change and value in a regular page, but how do I get those changes and update the adjacent dropdown.

$(document).on('change', 'select', function(){
      var data = $(this).val();
      alert(data);
});

Edit, Stephen request to show HTML

<tr>
  <td>
    //DropDown 1 (Imagine Country)
    <span class="projectcodeid"> 
      <select class="form-control" id="Records_1__TCode_Project_ID" name="Records[1].TCode.Project.ID"><option value=""></option>
       <option value="1">Plumbing</option>
       <option value="2">Modeling</option>
      </select></span>                                                    
  </td>

  <td>
    //DropDown 2 (Imagine State)
    <input type="hidden" name="Records.Index" value="1">
    <input class="timecodeid" name="Records[1].TCode.ID" type="hidden" value="5">                                                    
    <span class="timecode timecodeDdlId"> <select class="form-control timecodeDdlId" id="Records_1__TCode_ID" name="Records[1].TCode.ID"><option value=""></option>
</select></span>
    </td>

    <td>
        <input name="Records[1].DataRecords[0].ID" type="hidden" value="">
        <input class="hours" name="Records[1].DataRecords[0].Work" type="text" value="">
    </td>                                                

    <td>
       <input class="bs-checkbox" name="Records[1].DeleteRow" type="checkbox" value="true"><input name="Records[1].DeleteRow" type="hidden" value="false">
    </td>
 </tr>

Sample image for clarification

Share Improve this question edited Sep 8, 2021 at 17:15 Transformer asked Mar 12, 2017 at 8:44 TransformerTransformer 7,4393 gold badges28 silver badges57 bronze badges 10
  • Your need to show the html for a typical row (its just a matter of using class names and relative selectors) – user3559349 Commented Mar 12, 2017 at 8:45
  • you have to give different id to all element and fire change event on particular id element – bharat savani Commented Mar 12, 2017 at 8:49
  • @bharatsavani I have different Id, I can verify in code. But how do I attach change to it, since they all have different id's at runtime based on the db row? – Transformer Commented Mar 12, 2017 at 8:51
  • you have to pass id instead of select in jquery like below $(document).on('change', '#id', function(){ var data = $(this).val(); alert(data); }); – bharat savani Commented Mar 12, 2017 at 8:52
  • 2 @transformer, Please show the html for a typical row (and ignore the nonsense about id attributes) – user3559349 Commented Mar 12, 2017 at 8:59
 |  Show 5 more ments

2 Answers 2

Reset to default 3

Assuming that you can't identify dropdwns by class or anything. Assuming that every time you change the value in a dropdown you want to update the other dropdown on the same row. Assuming that you have only two dropdowns per row:

$('table').on('change', 'select', function() {
    var $current_dropdown = $(this),
        $other_dropdown = $(this).closest('tr').find('select').not(this);

    /// perform any task you need with current and other dropdown

});

You need to give both your <select> elements class names and use relative selectors to select the associated element in the same row.

Assuming your html is

<table id="table"> // give this an id attribute
    <tbody>
        <tr>
            <td><select class="country" ....> ..... </select></td>
            <td><select class="state" ....> ..... </select></td>
        </tr>
    </tbody>
</table>

Then your script will be

$('#table').on('change', '.country', function() {
    var selectedValue = $(this).val();
    var row = $(this).closest('tr'); // get the row
    var stateSelect = row.find('.state'); // get the other select in the same row
    // make you ajax call passing the selectedValue to your controller
    // in the success callback, update the options of stateSelect 
    $.ajax({
        url: ...
        data { id: selectedValue },
        ....
        success: function(data) {
            stateSelect.empty();
            $.each(data, function(item, index) {
                stateSelect.append($('<option></option>').val(iem.ID).text(item.Name));
            }
        }
    });
}

Refer also better way to load 2 dropdown in mvc for details of the code for populating cascading dropdownlists (consider caching the options as per the 2nd code example to avoid repeated ajax calls)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>