选择.change()
我有几个选择框,让用户输入我传递到另一个页面的时间:
<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>
我想要做的是,当“约会自”下的框更改时,“约会至”下的框的值也会更改到同一个。我已经成功地使用以下方法完成了此操作:
$('select[name="fromh"]').change( function() {
$('select[name="toh"]').val($(this).val());
});
这按预期工作。我现在想做的是更改分钟,但我不想将其更改为相同的值,而是希望它转到下一个。前任。我选择05以下的from,10以下的to将被选中。我尝试使用以下内容但没有成功:
$('select[name="fromm"]').change( function() {
$('select[name="tom"]').val($(this).val() + 1);
});
I have a few select boxes letting users input times that I pass on to the another page with:
<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>
What I want to do is that when a box under "Appointment From" is changed the value of the one under "Appointment To" to be changed to the same one. I have managed to accomplish this using the following:
$('select[name="fromh"]').change( function() {
$('select[name="toh"]').val($(this).val());
});
This works as expected. What I want to do now is change the minutes but instead of it being changed to the same value I would like it to go to the next one. Ex. I Pick 05 under from, 10 under to will be selected. I tried using the following but it didnt work:
$('select[name="fromm"]').change( function() {
$('select[name="tom"]').val($(this).val() + 1);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会使用 jQuery 的 filter 函数,该函数具有接受函数的重载。
或者为了真正安全:
I would use jQuery's filter function that has an overload which accepts a function.
Or to be really safe:
我不是专家,几乎不知道我实际上在做什么,但这也行得通吗?
也许需要一点逻辑来看看是否有下一个?
I'm no expert, hardly know what I'm doing actually, but wouldn't this work too?
Maybe need a little logic to see if there is a next?
您可以获取当前
select
中选中的索引,然后获取下一个选项的值。如果用户选择“55”,则滚动到零:JSFiddle: http://jsfiddle.net/eZBNG/1
You can get the selected index in the current
select
, then get the next option's value. Roll over to zero if the user selected "55":JSFiddle: http://jsfiddle.net/eZBNG/1