在我的下面的脚本中,我想将“2014”替换为我年变量的值,即“y”
我想删除值小于当前年份的选择框中的所有选项。以下脚本工作正常我需要帮助的是如何将代码中的“2014”替换为当前年份值?
感谢。
<script>
var d = new Date(),
n = d.getMonth()+1,
y = d.getFullYear();
$('.year option[value]').each(function() {
if( $(this).attr("value") < 2014 ) { $(this).remove(); };
});
</script>
答案 0 :(得分:0)
您需要在脚本加载之前使用document.ready,如下面的代码
<script>
var d = new Date(),
n = d.getMonth()+1,
y = d.getFullYear();
jQuery(document).ready(function(){
jQuery('.year option[value]').each(function() {
var optionVal = jQuery(this).attr("value");
if( optionVal < y )
{
jQuery(this).remove();
};
});
});
</script>