views:

204

answers:

2

Hi, i have a generated table that contains multiple rows with each row containing a leading td with a checkbox and another td with a select list =

<div class="table">
<table width="100%" class="tframe" id="table0">
<thead>
<tr class="trheadline">
    <th>Selection</th>
    <th>Repository-Tag</th>
</tr>
</thead>
<tbody>
<tr class="trow">
    <td>
        <input type="checkbox"
          value="0#298#abk_testn#l#CLEARCASE#at_web_vob"
          name="module">ModuleA
    </td>
    <td>
    <select name="cvstags">
        <option value="no_Repository">no_Repository</option>
        <option value="220607_143102110">220607_143102110</option>
        <option value="220607_09410236">220607_09410236</option>
        <option value="220507_091903814">220507_091903814</option>
    </select>
    </td>
</tr>
<tr class="trow">
    <td>
        <input type="checkbox"
          value="1#299#abk_integration#d#CLEARCASE#at_web_vob"
          name="module">ModuleB
    </td>
    <td>
    <select name="cvstags">
        <option value="220607_143102110">220607_143102110</option>
     </select>
    </td>
 </tr>
 <tr class="trow">
     <td>
        <input type="checkbox"
          value="2#301#abk_statcont##CLEARCASE#at_web_vob"
          name="module">ModuleC
    </td>
    <td>
    <select name="cvstags">
        <option value="no_Repository">no_Repository</option>
        <option value="220607_143102110">220607_143102110</option>
        <option value="220607_09410236">220607_09410236</option>
        <option value="220507_091903814">220507_091903814</option>
    </select>
        </td>
    </tr>
    </tbody>
 </table>
</div>

Now i want to pin a change event upon the select, but i want an action only to get started, if the leading checkbox is checked, for now i have =

<script>
    $('select').change(function(){
        alert($(this).val());
    });
</script>

which gives me the value of the selected option, fine but two problem remain =

  1. how to check whether the leading checkbox in the same tr is checked ?

    $('select').change(function(){
     if (the leading checkbox is checked) {
        run my action..
     }
    });
    
  2. the change event doesn't work if the select list has only one option as in the second row of my example above, how to achieve a similar behaviour as with change event for that case ?

Thanks for any hints !!

Regards, Gilbert

+1  A: 

How about looking at this the other way around i.e. handle the click event on the checkbox and if checked, assess the value of the select in the row?

$('tr.trow input:checkbox').click(function() {
    if (this.checked) {
        var self = $(this);
        self.parent().parent().find('select').val(); // value of select
    }
}

EDIT:

Here's how I would roughly handle it:

// use an object literal to prevent global pollution(!)
var rowActions = {
    checkbox: function() {
       if (this.checked) {
           rowActions.doSomething();
       }
    },
    select: function() {
       if ($(this).parent().parent().find('input:checkbox:checked').length) {
           rowActions.doSomething();
       }
    },
    doSomething: function() {
        alert('I did something!');
    }
}

$('tr.trow').each(function() {
    var self = $(this);
    self.find('input:checkbox').click(rowActions.checkbox);
    self.find('select').change(rowActions.select);
});

and here's a Working Demo to show it in action. add /edit to the url to play with the code.

Russ Cam
was my first ambition too, but the normal flow is like that = user decides which modules he wants to be delivered, so he checks the relating checkbox. Afterwards he chooses the appropriate tag, means he selects an option, often he changes his mind, means the checkbox stays selected but the selected options changes.. what would be the solution if i would go that way ??
Rebse
.. nevertheless could you post solution for the other way around ?
Rebse
Well, if there is only one option in the select for that row, the user cannot change their mind so we are safe to act when the user checks/unchecks the checkbox (note: use the click event). We can also bind an event handler to the change event on the select elements to run the same code as would run in the checkbox. So, extract that code out into a separate function and call it from inside the event handler in each case.
Russ Cam
that's a good shot for the single option problem, i'll give it a try..
Rebse
You made my day, thanks a lot for your snippet, works great !! :-)))i would like to vote, but i'm not allowed with only 11 reputation
Rebse
finally a vote for "how you would roughly handle it" because it works so smoothly
Rebse
If you have quite a few rows, you might consider using live() as opposed to bind() (I.e. click or change).
Russ Cam
A: 

Only have a second, but take a look at the prev method: e.g.

jQuery(this).prev("td").child("input[name=module]").val() != 'undefined'

... or something similar

And for the second part, you should add an empty-string option with a name like 'Select one...' which can be ignored, but will allow for your behavior to fire when the single selection is chosen.

RenderIn
hoped there would be another solution to the single selection problemseems like i have to go with a dummy option "Select one.." or similar
Rebse
a single option "selection" is an oxymoron :)
Russ Cam
Didn't try your suggestion, but i should work too, thanks!!There are several ways to Rome, i know some selectors should be preferred because of performance issues, but i'm far from being a jquery guru..
Rebse
update, did a test, didn't work =$('select').change(function(){ if ($(this).prev("td").child("input[name=module]").val() != 'undefined') { alert($(this).val()); }});
Rebse
I think you first need to select the parent TD of the SELECT, assuming that's what the "this" is in the context you write the query... e.g. jQuery(this).parent("td").prev("td").child("input[name=module]").val() to get the value of the checkbox
RenderIn
still not working, sorry
Rebse