提取在js中使用数组传递的selectbox文本

时间:2016-05-27 09:00:12

标签: javascript jquery html arrays select

我有这段代码:

//Create new Instance of Plate1 classes
for (int i = 0; i < rester.Character1.Length; i++)
{
    for (int j = 0; j < rester.Character1[i].Plate1.Length; j++)
    {
        rester.Character1[i].Plate1[j] = new Plate1();
    }
}

此数组位于:

<script>
var paesi = ["Austria","Belgium","Bulgaria","Croatia","Cyprus","Czech Republic","Denmark","Estonia","Finland","France","Germany","Greece","Hungary","Ireland","Italy","Latvia","Lithuania","Luxembourg","Malta","Netherlands","Poland","Portugal","Romania","Slovakia","Slovenia","Spain","Sweden","United Kingdom"];
var sel = document.getElementById('country');
for (var i = 0; i < paesi.length; i++) {
     var opt = document.createElement('option');
     opt.innerHTML = paesi[i];
     opt.value = paesi[i];
     sel.appendChild(opt);
}

我希望当我点击<div id="seleziona-nazione"><span><strong>Seleziona nazione</strong></span></div> <br /><br /> <br /><br /> <select id="country"></select> <button id="button1">seleziona</button> 时,button1中的文字被阅读,然后我就可以做一些动作了。我尝试这样,但不起作用:

selectedbox

function scelta() { document.getElementById("button1").addEventListener("click", function (paesi) { if (document.getElementById('country').text = 'Austria') { myLatLng = { lat: 47.516231, lng: 14.550072 }; centra(); }; }); }; 是一个全局变量。有人能帮我吗?

4 个答案:

答案 0 :(得分:1)

在点击事件中,您应使用value代替text,并在条件

中使用==代替=
document.getElementById("button1").addEventListener("click", function (paesi) {

  if (document.getElementById('country').value == 'Austria') {
    myLatLng = { lat: 47.516231, lng: 14.550072 };

    centra();
  };

});

https://jsfiddle.net/egtLvwvw/1/

答案 1 :(得分:1)

你可以这样做:

var paesi = ["Austria","Belgium","Bulgaria","Croatia","Cyprus","Czech Republic","Denmark","Estonia","Finland","France","Germany","Greece","Hungary","Ireland","Italy","Latvia","Lithuania","Luxembourg","Malta","Netherlands","Poland","Portugal","Romania","Slovakia","Slovenia","Spain","Sweden","United Kingdom"];

var sel = document.getElementById('country');
for (var i = 0; i < paesi.length; i++) {
  var opt = document.createElement('option');
  opt.innerHTML = paesi[i];
  opt.value = paesi[i];
  sel.appendChild(opt);
}


function doSomething(){
  var x = document.getElementById('country');
  var y = x.options[x.selectedIndex].text;
  if(y == 'Austria')
     console.log('Austria selected');

  }
<body>
  <div id="demo">
    <select id="country">
    </select>
    <input type="button" id="btnClick" onclick="doSomething()" value="Do something" />
  </div>
</body>

另外,我需要指出的是,如果你想比较值,你应该使用'=='进行非严格检查,使用'==='进行严格检查,你可以在'=='上阅读更多内容'==='这里:Which equals operator (== vs ===) should be used in JavaScript comparisons?

答案 2 :(得分:1)

你可以这样写:

document.getElementById("button1").onclick = function() {
    if (document.getElementById('country').value == 'Austria') {
        myLatLng = { lat: 47.516231, lng: 14.550072 };

        centra();
    };
}

答案 3 :(得分:0)

尝试使用onChange获取所选值。

<div id="seleziona-nazione">
    <span><strong>Seleziona nazione</strong>    
    </span></div> 
  <br /><br />  <br /><br />
   <select id="country" onchange="getComboA(this)></select>
   <button id="button1">seleziona</button>

在函数getComboA内,只要在下拉列表中更改选项,您就会得到所选的值。

function getComboA()
{
    //Keep the value in a variable. Use it on clicking on the button
}

或者你可以改变如下代码。使用.value代替.text

  document.getElementById("button1").on("click",function (paesi) 
 {

      if (document.getElementById('country').value == 'Austria') 
      {
          alert("Australia");
      };

});