1

我有这段代码可以获取 seletedIndex==1 并在 selectedindex==1 时使 div 可见,我想要做的是获取选择值而不是索引。请教我。我是 javascript 新手。if(document.frmregister.n_mode.selectedIndex==1) 就像这样将 selectedIndex==1 更改为 selectvalue?

function BID_RFQ() {
    if (document.frmregister.n_mode.selectedIndex == 1) {
        document.getElementById("bidrfq").style.visibility = 'visible';
        document.getElementById("bidrfq").style.overflow = 'visible';
    } else if (document.frmregister.n_mode.selectedIndex == 2) {
        document.getElementById("bidrfq").style.visibility = 'visible';
        document.getElementById("bidrfq").style.overflow = 'visible';
    } else {
        document.getElementById("bidrfq").style.visibility = 'hidden';
        document.getElementById("bidrfq").style.overflow = 'hidden';
    }
    return true;
}
4

3 回答 3

1

你正在寻找这个,我相信:

document.frmregister.n_mode.options[document.frmregister.n_mode.selectedIndex].value
于 2013-09-16T23:41:50.363 回答
0
<html>
  <head>
    <script>
function BID_RFQ() {
    var n_mode = document.getElementById('n_mode');
    var bidrfq = document.getElementById('bidrfq');

    console.log(n_mode);
    /* Here's the important part. */
    var selectedOptionValue = n_mode.options[n_mode.selectedIndex].value;

    var visibility='visible',overflow='visible';

    if (selectedOptionValue  != 'One' && selectedOptionValue!='Two') {
      visibility=overflow='hidden';
    }

    bidrfq.style.visibility = visibility;
    bidrfq.style.overflow = overflow;

    return true;
}
    </script>
  </head>
  <body>
   <form id='frmregister'>
    <select id='n_mode' onchange='BID_RFQ()'>
      <option>One</option>
      <option>Two</option>
      <option>Three</option>
    </select>
    </form>

    <div id="bidrfq">bidrfq</div>
  </body>
</html>
于 2013-09-16T23:49:29.653 回答
0

如果您想要value所选选项的,您可以使用元素的.value属性来获取它。select

console.log(document.frmregister.n_mode.value);

这将自动为您.value提供所选option元素的 ,或者如果没有value定义,它将为您.text提供option.

演示:http: //jsfiddle.net/WdMTJ/


请注意,正如下面的@RobG 所述,.text如果<option>元素没有value属性,IE8 及更低版本将不会返回。因此,您需要确定已明确包含这些值。

于 2013-09-16T23:41:09.970 回答