1

我试图让单个选择(与其他选择具有相同的类)来响应 .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>
4

5 回答 5

4

更改事件处理程序仅添加一次(在文档准备好时)。您应该使用 $.delegate 或 $.on 将事件附加到稍后添加到页面的元素。例如,以下内容应该可以工作:

$(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);
});

$('#monday .events').on("change", ".setto", 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>')
        }
    });
});
于 2012-11-04T04:55:39.213 回答
1

就像 Yatrix 所说,您的第一个选择缺少class="setto"。考虑到这一点,您还必须像这样绑定更改事件。

$('#monday .event').on('change', '.setto', function() {
于 2012-11-04T04:57:51.727 回答
1

尝试更改此“选项:选定”的顺序...

if($(this,"option:selected").val() == "specifiedtime") {

 ... do stuff ...     

更新@Yatrixs 建议解决了持久性错误。

于 2012-11-04T05:07:31.817 回答
1

你只是将它应用于一个。只有一个选择setto应用了该类,这就是您正在绑定的操作。

$(document).on('theEvent','theSelector', function () {
    DoKungfu();
});

是你的朋友。这会将事件绑定到 DOM 中当前与选择器匹配的任何项目以及以后添加的任何与其匹配的项目。

于 2012-11-04T04:54:37.733 回答
-1

追加后再次尝试绑定事件:

$('#monday .events').append(neweventstring);
于 2012-11-04T04:52:47.670 回答