【发布时间】:2012-10-22 10:54:30
【问题描述】:
我有一个表单,用户可以在其中选择时间间隔。但我只希望他们只能选择一个选项。在从下拉列表中选择一个值时,jQuery 应该重置其他下拉列表。 HTML(和 php,但不相关):
<select class="span1" name="repeatminute" id="repeatIntervalTiming" onChange="disableOther(this)">
<?php for($i=0; $i<61;$i++){ echo "<option>$i</option>"; } ?>
</select>
minute(s)</label>
<label>
<select class="span1" name="repeathour" id="repeatIntervalTiming" onChange="disableOther(this)">
<?php for($i=0; $i<25;$i++){ echo "<option>$i</option>"; } ?>
</select>
hour(s)</label>
<label>
<label>
<select class="span1" name="repeatday" id="repeatIntervalTiming" onChange="disableOther(this)">
<?php for($i=0; $i<32;$i++){ echo "<option>$i</option>"; } ?>
</select>
day(s)</label>
<label>
<select class="span1" name="repeatmonth" id="repeatIntervalTiming" onChange="disableOther(this)">
<?php for($i=0; $i<13;$i++){ echo "<option>$i</option>"; } ?>
</select>
month(s)</label>
这是我的 jQuery 尝试:
function disableOther(caller){
$('#repeatIntervalTiming').each(function(index) {
if ($(this).text() != caller.textContent){
$(this).val(0);
}
});
【问题讨论】:
-
首先不要为所有
<select>提供相同的ID。 -
id 应该始终是唯一的!改用类。
标签: javascript jquery