血泪奋斗
作者血泪奋斗·2017-09-07 11:35
商业智能工程师·地球村

两种js方法实现获取默认的年月

字数 2063阅读 1626评论 0赞 3

注意:这个需要在提示页面中加载,从而传到页面;

<span id ="y" >
。。。。
这儿是你的年和月的值提示
。。。。
</span>

<script>
function init(){
var theA = document.getElementById ("y");
var theB = theA.getElementsByTagName("select");
theB[0].remove(0);
theB[0].remove(0);
theB[1].remove(0);
theB[1].remove(0);
var cur_date = new Date();
var cur_year = cur_date.getFullYear();
var cur_month = cur_date.getMonth()+1;
for (var i = 0; i <theB[0].length; i++) { //循环搜索下拉列表中和当前年份caption相等的值
var temp = theB[0].options[i].text;
//alert(temp);
if (temp == cur_year) {
//alert('Find!!! ' + temp + ' ' + i);
theB[0].options[i].selected = true;
break;
}
}
for (var i = 0; i <theB[1].length; i++) { //循环搜索下拉列表中和当前年份caption相等的值
var temp = theB[1].options[i].text;
//alert(temp);
if (temp == cur_month) {
//alert('Find!!! ' + temp + ' ' + i);
theB[1].options[i].selected = true;
break;
}
}
promptButtonFinish();

}

</script>

<body onload=init()>

这样就自动提交了,参数从提示页面传到页面。

方法二:

<script language="javascript">

var f = getFormWarpRequest();
var list = f._oLstChoicesPromptYear;//PromptYear是组件的自定义名称
var list1 = f._oLstChoicesPromptMonth;//PromptMonth是组件的自定义名称

var cur_date = new Date();
var cur_year = cur_date.getFullYear();
var cur_month = cur_date.getMonth()+1;
list.remove(1);
list.remove(0);
list.removeAttribute("hasLabel");
list1.remove(1);
list1.remove(0);
list1.removeAttribute("hasLabel");


for (var i = 0; i <list.length; i++) {  //循环搜索下拉列表中和当前年份caption相等的值
        var temp = list.options[i].text;
        //alert(temp);
                       if (temp == cur_year) {
            //alert('Find!!! ' + temp + ' ' + i);
                               list.options[i].selected = true;
            break;
                       }
              }

for (var i = 0; i <list1.length; i++) { //循环搜索下拉列表中和当前年份caption相等的值

        var temp = list1.options[i].text;
        //alert(temp);
                       if (temp == cur_month) {
            //alert('Find!!! ' + temp + ' ' + i);
                               list1.options[i].selected = true;
            break;
                       }
              }

</script>

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

3

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关问题

X社区推广