如何显示按钮上的选择框选项单击?

时间:2016-12-09 03:35:00

标签: javascript jquery

我想在按钮上显示选择框选项。不在选择框下拉菜单上。当我点击按钮时,下拉列表将显示出来。

更新的 我的意思是。单击下拉框时,将显示确认框。然后,当我点击取消时,没有任何显示..当我点击OK时,下拉列表将会显示。

这是我的代码

<select id="mySelect" name="myselect" onclick="myFunction()">
<option value="Audi">Audi
<option value="BMW">BMW
<option value="Mercedes">Mercedes
<option value="Volvo">Volvo
</select>

 <p id="demo"></p>

 <script>
 function myFunction() {
   var txt;
   var select=document.getElementsByName("myselect");
   var r = confirm("Press a button!");
   if (r == true) {
      select.show();
      txt = "You pressed OK!";
   } else {
      txt = "You pressed Cancel!";
   }
      document.getElementById("demo").innerHTML = txt;
  }
   </script>

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

data$refOFFSET <- NA #initialize column called refOFFSET

for (i in 1:length(data$ID)){
        if (!length(which(data$ID==(data$ID[i]) & data$AA_FIRST==1))) { #if it's integer0
                next #go on to next i
        }else{
                tmpval <- data$OFFSET[which(data$ID==(data$ID[i]) & data$AA_FIRST==1)]} 
        data$refOFFSET[i] <- tmpval #create column whose value is equal to the reference OFFSET for each ID (i.e. the OFFSET where AA_FIRST=1)
} 
function myFunction(item) {
   var txt;
   var dropDown=document.getElementsByName("myselect");
   var r = confirm("Press a button!");
   if (r == true) {      
     item.setAttribute("size", item.options.length);
      txt = "You pressed OK!";
   } else {
     
      txt = "You pressed Cancel!";
   }
      document.getElementById("demo").innerHTML = txt;
  }

答案 1 :(得分:0)

我认为不可能完全模仿您的预期功能;显示同步confirm对话框似乎将重点放在select元素上。但是,我能够做到这一点,以便您可以在对对话框说好之后自己打开选择。

&#13;
&#13;
var demo = document.getElementById('demo'), ignore = false

document.getElementById('mySelect').addEventListener('focus', function () {
  if (ignore) {
    ignore = false
  } else if (confirm('Press a button!')) {
    demo.textContent = 'You pressed OK!'
    ignore = true
  } else {
    demo.textContent = 'You pressed Cancel!'
  }
})
&#13;
<select id="mySelect" name="myselect">
  <option value="Audi">Audi</option>
  <option value="BMW">BMW</option>
  <option value="Mercedes">Mercedes</option>
  <option value="Volvo">Volvo</option>
</select>
<p id="demo"></p>
&#13;
&#13;
&#13;