选择.change()

Mei*_*lla 4 html javascript forms jquery select

我有几个选择框让用户输入我传递到另一个页面的时间:

<p class="apptmar">
    <label for="from">Appointment Starts:</label><br />
    <input type="text" name="from" id="from" class="appt" /><br />
    <select name="fromh" class="apptselect">
        <option>1</option>
        ...
        <option>12</option>
    </select>
    <select name="fromm" class="apptselect">
        <option>00</option>
        <option>05</option>
        ...
        <option>55</option>
    </select>
    <select name="froma" class="apptselect">
        <option>AM</option>
        <option>PM</option>
    </select>
</p>
<p class="apptmar">
    <label for="to">Appointment Ends:</label><br />
    <input type="text" name="to" id="to" class="appt" /><br />
    <select name="toh" class="apptselect">
        <option>1</option>
        ...
        <option>12</option>
    </select>
    <select name="tom" class="apptselect">
        <option>00</option>
        ..
        <option>55</option>
    </select>
    <select name="toa" class="apptselect">
        <option>AM</option>
        <option>PM</option>
    </select>
</p>
Run Code Online (Sandbox Code Playgroud)

我想要做的是,当"约会来自"下的一个框被改变时,"约会到"下​​的一个框的值将被改为相同的一个.我已成功使用以下方法完成此操作:

$('select[name="fromh"]').change( function() {
    $('select[name="toh"]').val($(this).val());
});
Run Code Online (Sandbox Code Playgroud)

这按预期工作.我现在要做的是更改分钟,但不是将其更改为相同的值,我希望它转到下一个.防爆.我挑选05下,10下将被选中.我尝试使用以下但它没有工作:

$('select[name="fromm"]').change( function() {
    $('select[name="tom"]').val($(this).val() + 1);
});
Run Code Online (Sandbox Code Playgroud)

Ada*_*kis 5

我会使用jQuery的过滤函数,它具有接受函数的重载.

$('select[name="fromm"]').change( function() {
    var $that = $(this);

    var $targetNode = $('option', 'select[name="tom"]').filter(function() {
        return $(this).text() === $that.val();
    }).next();
    $('select[name="tom"]').val($targetNode.text());
});
Run Code Online (Sandbox Code Playgroud)

或者真的很安全:

$('select[name="fromm"]').change( function() {
    var $that = $(this);

    var $targetNode = $('option', 'select[name="tom"]').filter(function() {
        return $(this).text() === $that.val();
    }).next();

    if ($targetNode.length === 1)
        $('select[name="tom"]').val($targetNode.text());
});
Run Code Online (Sandbox Code Playgroud)