1
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?

4

3 回答 3

1

在js中使用这个:

   var x = document.getElementById('MY_DIV').name;
   x = x.toLowerCase(); //use this line
   document.getElementById(x).value=2;
于 2013-09-23T09:44:44.293 回答
0

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

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

参考tutorialsPoint使用 ignoreCase 属性的示例

<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 属性

为 DK 功能更新代码

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;
   
}
于 2013-09-23T09:47:00.417 回答
0

JavaScript 区分大小写,您可以使用以下内容,但如果您的目标 div id 包含任何大写字符,它将不起作用

var x = document.getElementById('MY_DIV').name.toLowerCase();
document.getElementById(x).value=2;
于 2013-09-23T09:48:32.520 回答