我正在尝试找到在更改选择框时发生不同功能的最佳方法。
这是我选择的 HTML
<select name="event-recurring">
<option selected="true">None</option>
<option value="daily">Daily</option>
<option value="weekly">Weekly</option>
<option value="monthly">Monthly</option>
</select>
我的jQuery是:
$('select[name=event-recurring] option:selected').change (function (){
//Will Do Something
});
我如何告诉它在不同的值上做不同的事情?
解决方案:
$('#event-recurring').change(function (){
var $select = $(this);
switch($select.val()) {
case "daily":
$('#recurring-options').remove();
$("<div id='recurring-options' class='row'><label>Choose:</label>Choose a Date</div>").insertAfter('#recurring');
break;
case "weekly" :
$('#recurring-options').remove();
$("<div id='recurring-options' class='row'><label>Choose:</label>Weekly</div>").insertAfter('#recurring');
break;
case "monthly":
$('#recurring-options').remove();
$("<div id='recurring-options' class='row'><label>Choose:</label>Monthly</div>").insertAfter('#recurring');
break;
default:
$('#recurring-options').remove();
break;
}
});