var result = window.showModalDialog('/lcrm/Common/calendar.aspx?value=', 'window', 'dialogHeight: 300px;dialogWidth:400px;resizable=on;scroll=on;');
if (result != undefined) {                document.getElementById('txtSTime').value = result.substring(0, 6);
               
               
            }
我调用了个时间控件,在文本里只显示年和月 当我选择的日期为2008-8-7 时,显示正确 但我日期选择为2008-11-1 时,结果却显示为2008-1 请问我要怎么写才能同时满足两个条件呢?我知道有种方法可以实现,那就是判断下可以实现效果,但我不想那样写,请问还有什么办法可以一次性就出来结果不?

解决方案 »

  1.   

    在后台代码中赋值,txtSTime.Text.ToString("yyyy-MM-dd");
      

  2.   


    result.substring(0, 6);
      

  3.   

    或这样赋值
    js中result.substring(0, 7);
      

  4.   


    str="2010-7-29";
    arr=str.split("-");
    alert(arr[0]+"-"+arr[1])
      

  5.   

    lastIndexOf() 从后向前搜索字符串。 
      

  6.   

    document.getElementById('txtSTime').value = result.substring(0, result.ToString().LastIndexOf("-"));
    //截取最后一个 “-”之前的字符 即可
      

  7.   


    var a = result.substring(6, 7);
    if(isNaN(a)){
        document.getElementById('txtSTime').value = result.substring(0, 6);
    }
    else
    {
        document.getElementById('txtSTime').value = result.substring(0, 7);
    }
      

  8.   

    var a = result.substring(6, 1);
      

  9.   

    document.getElementById('txtSTime').value = result.substring(0, result.ToString().LastIndexOf("-"));
    //截取最后一个 “-”之前的字符 即可
    就这样就可以了啊 哪用那么麻烦啊
      

  10.   


    根据“-”分割不就可以了,拿第一个和第二个就可以了str="2010-7-29";
    arr=str.split("-");
    alert(arr[0]+"-"+arr[1])
      

  11.   

    怎么可能呢,substring 第一个S大写
      

  12.   


            function GetTime() {
                var result = window.showModalDialog('/lcrm/Common/calendar.aspx?value=', 'window', 'dialogHeight: 300px;dialogWidth:400px;resizable=on;scroll=on;');
                //debugger
                if (result != undefined) {                document.getElementById('txtSTime').value = result.Substring(0, result.LastIndexOf("-"));
                   
                }
                else {
                    return false;
                }
            }不是在后台实现。。
      

  13.   

    String.LastIndexOf 方法
    String.LastIndexOf (Char) 报告指定 Unicode 字符在此实例中的最后一个匹配项的索引位置 如:
      string a="2010-12-1";
      string b="2010-9-2";
      string aa=a.Substring(0,a.LastIndexOf("-"));
      string bb=b.Substring(0,b.LastIndexOf("-"));
       Response.Write(a.LastIndexOf("-")); //结果为7
      Response.Write(a.LastIndexOf("-")); //结果为6
      Response.Write(aa);  //结果为:2010-12
      Response.Write(bb);  //结果为:2010-9楼主 还要我这样给你解释啊
      

  14.   

    出来了,谢谢各位了哈,是个大小写问题LastIndexOf 改成 lastIndexOf 就行了
    完整语句为document.getElementById('txtSTime').value = result.substring(0,result.lastIndexOf("-"));
      

  15.   

    原来你要的是JS的代码,不过跟asp.net 一样