Find Value Based on Input and Lookup Table

2019-09-14 23:56发布

问题:

I've a jQuery problem that I could use some help with. I'm trying to determine a value based on values in table.

Here's the scenario:

  1. A category is selected. This determines the column to use. Here's the code for that:

    var column;
    
    if( $('input:radio[name=arc2-1]:checked').val() == 0 && $('input:radio[name=arc2-2]:checked').val() == 0) {
    column = 2;
    }else if( $('input:radio[name=arc2-1]:checked').val() == 0 && $('input:radio[name=arc2-2]:checked').val() == 1) {
    column = 3;
    }else if( $('input:radio[name=arc2-1]:checked').val() == 1 && $('input:radio[name=arc2-2]:checked').val() == 0) {
    column = 4;
    }else if( $('input:radio[name=arc2-1]:checked').val() == 1 && $('input:radio[name=arc2-2]:checked').val() == 1) {
    column = 5;
    }else if( $('input:radio[name=arc2-1]:checked').val() == 2 && $('input:radio[name=arc2-2]:checked').val() == 1) {
    column = 6;
    }else if( $('input:radio[name=arc2-1]:checked').val() == 3 && $('input:radio[name=arc2-2]:checked').val() == 1) {
    column = 7;
    }
    
  2. There's a hidden table in the HTML that looks like this:

    <table id="ms25">
        <tr>
            <td>65</td>
            <td>5.4</td>
            <td>5.4</td>
            <td>3.1</td>
            <td>3.1</td>
            <td>3.0</td>
            <td>-</td>
        </tr>
        <tr>
            <td>55</td>
            <td>4.6</td>
            <td>4.6</td>
            <td>2.7</td>
            <td>2.7</td>
            <td>2.5</td>
            <td>-</td>
        </tr>
        <tr>
            <td>45</td>
            <td>3.8</td>
            <td>3.8</td>
            <td>2.2</td>
            <td>2.2</td>
            <td>2.0</td>
            <td>-</td>
        </tr>
        <tr>
            <td>35</td>
            <td>2.9</td>
            <td>2.9</td>
            <td>1.7</td>
            <td>1.7</td>
            <td>1.5</td>
            <td>8.1</td>
        </tr>
    </table>
    

The values in the tables don't change. And there will always be 7 columns and 11 rows (I shortened the example).

  1. Here's the tricky part. A number (65 or less) is entered in an input field. Then:

  2. The input number is checked against the first table column. The input number is "rounded up" to the next nearest number above it in the column.

  3. The number to find is the one from step 1 and step 4.

For example, if the column is 4 and the number entered is 31, you'd go to the first column and find the next nearest number "rounded up" (35), then you go over to column 4...and the number to find is 1.7.

I've found bits and pieces that relate to a solution (such as this: jQuery find same td in prev. and next row), but I'm unsure as to how to put it all together.

Any help/guidance as to a solution or where to look for one would be much appreciated.

回答1:

You can do it like this:

function get_table_value(column, value) {
   table = $('#ms25');
   smallestdiff = Number.MAX_VALUE;
   savedindex = 0;
   table.find('td:first-child').each(function(i) {
      diff = parseInt($(this).text(),10) - parseInt(value, 10);
      if (diff > 0 && diff < smallestdiff) {
          smallestdiff = diff;
          savedindex = i;
      }
   });
   tr = table.find('tr:eq('+savedindex+')');
   alert ( tr.find('td:first-child').text() );  //alerts 35
   alert ( tr.find('td:nth-child('+column+')').text() ); //alerts 1.7
}

See working demo