0

我有一个下拉列表。当某人单击下拉列表中的“其他”选项时,会出现一个空白行,允许某人编写他或她自己的请求。我想创建一个警告框,以粗体字显示某人在空白行中所写的内容。我该怎么做呢?

这是我的代码:

<form action="">
    <select name="requests" onchange ="checkIfOther();" id="dropDown1">
    <option value="blank"></option>
    <option value="good morning sir">Good Morning Sir</option>
    <option value="temperature">The current temperature is _____ centigrade.</option>
    <option value="other">Other</option>    
</select>
</form>

 </p>
 <button onclick="myFunction()" value>Submit</button>

     <div id="other" style="display:none">  
         <br><br><label>Optional Request: </label><input type="text" id="otherText"/>

         </div>

</body>
</html>

<script>
   function checkIfOther(){
         a = document.getElementById("dropDown1");        
         if(a.value == "other")           
             document.getElementById("other").setAttribute("style","display:inline");
        else
             document.getElementById("other").setAttribute("style","display:none");
         }
</script>

<script>
   function myFunction(){
       var x=document.getElementById("dropDown1");
       alert("You have chosen: " + x.options[x.selectedIndex].text);
       if(x.value == "other"){
           b=document.getElementById("other"); 
               alert("You have chosen: " + b.text); //what do I do?
               }   
           }
</script>
4

2 回答 2

0

尝试使用 Jquery,代码如下:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>

function myFunction(){
    alert('You have chosen: '+$("#otherText").val());
}

function checkIfOther()
{
    $("#otherText").val('');
    ($("#dropDown1").val() == 'other') ?  $("#other").show() :  $("#other").hide();
}

</script>

让我知道这有帮助吗?

于 2013-07-04T10:27:29.173 回答
0

但我做了这样的事情......

<script type="text/javascript">
    function mySubmit(){
        var x=document.getElementById("dropDown1");
        if(x.value == "other"){
            alert("You have chosen: " + otherText.value); 
        }
        else {  
            alert("You have chosen: " + x.options[x.selectedIndex].text);
        }
    }   
</script>
于 2013-07-05T05:05:33.427 回答