简体   繁体   English

从下拉列表中获取值时出错

[英]Getting error while retrieving value from dropdown

I have a made a simple jsp page which just includes a single dropdown page jsut for testing purposes . 我制作了一个简单的jsp页面,其中仅包含一个用于测试目的的下拉页面jsut。 i have put a small condition in the dropdown but it is not running with the condition 我在下拉菜单中放置了一个小条件,但没有与条件一起运行

the code is as follows 代码如下

<html>
<body>
    <form method="get" name="form">  
    <select name="color">
        <option value="empty">Select Color</option>
        <option value="a">red</option>
        <option value="b">blue</option>
        <option value="c">green</option>
        <option value="d">yellow</option>
    </select>
    <input type="submit" value="Submit">
    </form>
</body>
</html>

<%  String color = request.getParameter("color");  
    if(color == "empty"){
      out.println("blah blah blah");   
    }
%>    

nothing gets printed which means the if condition is not running can somebody help here ? 什么都不会打印出来,这意味着if条件未运行,有人可以在这里提供帮助吗?

I don't think if(color == "empty") is how you compare Strings in java. 我不认为if(color == "empty")是如何比较Java中的字符串。 It should be if(color.equals("empty")) 应该是if(color.equals("empty"))

You can use following code : 您可以使用以下代码:

<select name="color" onchange="getComboA(this)>
    <option value="empty">Select Color</option>
    <option value="a">red</option>
    <option value="b">blue</option>
    <option value="c">green</option>
    <option value="d">yellow</option>
</select>

in javascript : 在javascript中:

<script>
  function getComboA(sel) {
    var value = sel.value;
    if(value == "empty"){
        alert("Blah Blah");
    }
  }
</script>

I think that the scriptlet( code between <%%>) is executed when the page is executed, so with this code, you can't get any parameter because the form wasn't submitted yet... You can do something like 我认为scriptlet(<%%>之间的代码)是在执行页面时执行的,因此使用此代码,您无法获得任何参数,因为尚未提交表单...您可以执行类似的操作

 <select name="color" onchange="printValue(this)">

then on some script tag use the value like 然后在某些脚本标签上使用类似

    function printValue(sel){
      var value = sel.value;
      alert("selected : " + value);
    }

or you can try to use the method location.reload(true) or maybe window.location.reload(true); 或者您可以尝试使用location.reload(true)方法或window.location.reload(true);方法。 (true to be reloaded from the server) but I never used this one. (从服务器重新加载是正确的),但我从未使用过此服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM