题
我试图找到对我的选择框的变化不同的功能发生的最好办法。
这是我的选择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;
}
});
解决方案
$('select[name=event-recurring]').change (function (){
var $select = $(this);
switch($select.val()) {
case "daily":
// do something
break;
case "weekly" :
// do something else
break;
default:
// default case
break;
}
});
我建议你给<select>
一个ID也是一样,用它来选择它,因为它会比按名称更快
其他提示
相当肯定,这将改变函数内部工作:
$('select[name=event-recurring]').change (function (){
switch ($('option:selected', this).val()) {
case "daily":
// do something
break;
case "weekly":
// do something
break;
case "monthly":
// do something
break;
}
});
你是指做不同选择不同的价值的东西?
如果这样,相信可以使用“这”对象在功能指的事件(在这种情况下,选择)的源,并且可以使用“.VAL()”或”的.text检查其值( )”看什么是与所述相应值的过程。
希望这有助于。
在选定的值使用子查询,并得到它的价值属性作为鉴别为您的操作:
$('select[name=event-recurring]').change( function() {
var value = $(this).find(':selected').attr('value');
if (!value) { // none
do something
}
else if (value == 'daily') {
do something else
}
...
});
不隶属于 StackOverflow