[英]display selected value of the drop down in jsp page
I want to display selected value. 我想显示选定的值。 In the text field I can display it within value like below
在文本字段中,我可以将其显示在如下所示的值内
value ="<%=event_data.getE_venue()%>"
code : 代码:
<input type="text" name="where" placeholder="Add a place" size="23" value ="<%=event_data.getE_venue()%>"/>
<select name="category" value ="<%=event_data.getE_target_category()%>" id="single1">
<option>Sports</option>
<option>Corporate</option>
<option>Religious</option>
<option>Music</option>
</select>
but in dropdown box it doesn't work. 但在下拉框中它不起作用。 please help me.
请帮我。 thanks..
谢谢..
Firstly, select doesn't work in that way , you need to put selected attribute in option that matches your input. 首先,select不能以这种方式工作,您需要将selected属性放入与您的输入匹配的option中。
for example: <option selected='selected'>Sports</option>
例如:
<option selected='selected'>Sports</option>
check this fiddle : 检查这个小提琴:
http://jsfiddle.net/ZLTS7/ http://jsfiddle.net/ZLTS7/
your code should be something like : 您的代码应类似于:
<input type="text" name="where" placeholder="Add a place" size="23" value ="<%=event_data.getE_venue()%>"/>
<select name="category" id="single1">
<option <%= (event_data.getE_target_category().equals("Sports")?"selected='selected'":"") %>>Sports</option>
<option <%= (event_data.getE_target_category().equals("Corporate")?"selected='selected'":"") %>>Corporate</option>
<option <%= (event_data.getE_target_category().equals("Religious")?"selected='selected'","") %>>Religious</option>
<option <%= (event_data.getE_target_category().equals("Music")?"selected='selected'":"") %>>Music</option>
</select>
You need to change the dropdown value through Javascript or jQuery. 您需要通过Javascript或jQuery更改下拉值。 You can assign desired value to dropdown just like input
您可以像输入一样为下拉列表指定所需的值
var dd = document.getElementById('single1');
var opts = ddl.options.length;
var value = <%=event_data.getE_venue()%>;
for (var i=0; i<opts; i++){
if (dd.options[i].value == value){
dd.options[i].selected = true;
break;
}
}
or if you are using jQuery. 或者如果你使用jQuery。
$("#single1").val(value);
See this example: 看这个例子:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#slectboxid option').click(function(){
$('#textboxid').val($(this).val());
});
});
</script>
</head>
<body>
<form action="#" method="post">
<select name="select" id="slectboxid">
<option value="test">test</option>
<option value="test2">test2</option>
</select>
<input type="text" name="text" id="textboxid" />
</form>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.