我试图让单个选择(与其他选择具有相同的类)来响应 .change 函数,但是它仅适用于其中一个选择。如果您测试此代码,它将更有意义。尝试添加一些“ON/OFF events”,然后在各种选择中选择“Specified Time”。你会看到只有第一个响应。有任何想法吗?
谢谢!
请看以下代码:
$(document).ready(function() {
var neweventstring = '<div class="event">Turns <select name="ONOFF"><option value="On">ON</option><option value="Off">OFF</option></select> at: <select name="setto" class="setto"><option>Select Time</option><option value="Sunrise">Sunrise</option><option value="Sunset">Sunset</option><option value="specifiedtime">Specified Time</option></select></div>';
$('#addmondaysevent').click(function () {
$('#monday .events').append(neweventstring);
});
$('.setto').change(function() {
alert('The function is called');
if($("option:selected", this).val() =="specifiedtime"){
alert('If returns true');
$(this).css("background-color", "#cc0000");
$(this).after('<div class="specifictime"><input type="text" value="00" style="width:30px"/> : <input type="text" value="00" style="width:30px"> <select name="ampm"><option value="AM" selected>AM</option><option value="PM">PM</option></select></div>')
}
});
});
还有我的 HTML:
<div id="monday">
<h2>Mondays</h2>
<div class="events">
<div class="event">
Turn
<select name="ONOFF">
<option value="On">ON</option>
<option value="Off">OFF</option>
</select>
at:
<select name="setto" class="setto">
<option>Select Time</option>
<option value="Sunrise">Sunrise</option>
<option value="Sunset">Sunset</option>
<option value="specifiedtime">Specified Time</option>
</select>
</div>
[<a id="addmondaysevent">Add an ON/OFF event</a>]
</div>
</div>