如何在js中匹配两个id时使用ignorecase

时间:2013-09-23 09:41:28

标签: javascript case-sensitive

function myfunc()
{

   var x = document.getElementById('MY_DIV').name;
   document.getElementById(x).value=2;
}


      <input type="text" id="MY_DIV" name="MY_DIV1"/>
      <input type="text" id="my_div1" />
      <input type="button" name="submit" value="submit" onclick="myfunc();">

如何在上面的js代码中使用ignorecase来填充第二个文本框中的值2?

3 个答案:

答案 0 :(得分:1)

在js:

中使用它
   var x = document.getElementById('MY_DIV').name;
   x = x.toLowerCase(); //use this line
   document.getElementById(x).value=2;

答案 1 :(得分:0)

  

Javascript字符串不区分大小写的比较可以使用   的 string.toUpperCase。

var x = document.getElementById('MY_DIV').name;
x = x.toUpperCase(); //use this line
    if (x == "STRING TO MATCH"){
    }

使用ignoreCase属性

引用tutorialsPoint Example
<html>
<head>
<title>JavaScript RegExp ignoreCase Property</title>
</head>
<body>
<script type="text/javascript">
   var re = new RegExp( "string" );

   if ( re.ignoreCase ){
      document.write("Test1-ignoreCase property is set"); 
   }else{
     document.write("Test1-ignoreCase property is not set"); 
   }
   re = new RegExp( "string", "i" );

   if ( re.ignoreCase ){
      document.write("<br/>Test2-ignoreCase property is set"); 
   }else{
     document.write("<br/>Test2-ignoreCase property is not set"); 
   }
</script>
</body>
</html>

输出

  

Test1 - 未设置ignoreCase属性

     

Test2 - 设置ignoreCase属性

为D.K功能更新了代码

function myfunc()
{

    var x = document.getElementById('MY_DIV').name;
    //x = x.toUpperCase();   // check the result with / without un-commenting this line

    var re = new RegExp( x );

    if ( re.ignoreCase ){
      document.write("X - Test1-ignoreCase property is set"); 
    }else{
     document.write("X - Test1-ignoreCase property is not set"); 
    }
    re = new RegExp( x, "i" );

    if ( re.ignoreCase ){
      document.write("<br/> X - Test2-ignoreCase property is set"); 
    }else{
     document.write("<br/>X - Test2-ignoreCase property is not set"); 
    }
    x = x.toUpperCase();  // ignoring case
    document.getElementById(x).value=2;

}

答案 2 :(得分:0)

JavaScript区分大小写您可以使用以下内容,但如果目标div ID包含任何大写字符,则无法使用

var x = document.getElementById('MY_DIV').name.toLowerCase();
document.getElementById(x).value=2;