我有几个选择框,让用户输入我传递给另一个页面的时间:
<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>我想要做的是,当"Appointment“下的框被更改时,"Appointment”下的值也将更改为相同的值。我已经通过以下方法实现了这一点:
$('select[name="fromh"]').change( function() {
$('select[name="toh"]').val($(this).val());
});这与预期的一样。我现在想要做的是更改分钟,但不是将其更改为相同的值,而是希望它转到下一个值。例如。我选择05下的自,10下的至将被选中。我试着使用下面的方法,但不起作用:
$('select[name="fromm"]').change( function() {
$('select[name="tom"]').val($(this).val() + 1);
});发布于 2011-12-08 05:17:15
我会使用jQuery的filter函数,它有一个接受函数的重载。
$('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());
});或者为了真正的安全:
$('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());
});发布于 2011-12-08 05:22:36
您可以在当前select中获取所选索引,然后获取下一个选项的值。如果用户选择了“55”,则将鼠标移到零:
$('select[name="fromm"]').change(function() {
var index = (this.options.selectedIndex+1)%this.options.length;
$('select[name="tom"]').val(this.options[index].value);
});JSFiddle:http://jsfiddle.net/eZBNG/1
发布于 2011-12-08 05:23:54
我不是专家,几乎不知道我在做什么,但这不也可以吗?
$('select[name="fromm"]').change( function() {
$('select[name="tom"]').val($(this).next().val());
});也许需要一点逻辑来看看是否有下一个?
https://stackoverflow.com/questions/8422538
复制相似问题