如果会话中没有任何值,那么我想显示选择选项。
<select class="selectpicker show-tick" id="vehicle" data-live-search="true" required="required">
<option data-tokens=""><%=session.getAttribute("operator1") %></option>
<option data-tokens="Select option">Select option</option>
<option data-tokens="car">Car</option>
<option data-tokens="bike">Bike</option>
<option data-tokens="truck">Truck</option>
当前,如果null
中没有值,它将显示Session
。我已经使用了此代码,但无法正常工作。
window.onload = function()
{
var DropdownList = document.getElementById('vehicle');
var SelectedIndex = DropdownList.selectedIndex;
var SelectedValue = DropdownList.value;
var SelectedText = DropdownList.options[DropdownList.selectedIndex].text;
if (SelectedText == null)
{
DropDownList.SelectedValue == Select option;
}
else
{
// otherwise display session value
}
}
答案 0 :(得分:1)
这只能通过在JSP表达式标记中使用三元运算符来实现,并且避免编写JavaScript。
使用:
<option data-tokens=""><%= session.getAttribute("operator1") != null ? session.getAttribute("operator1") : "Select option" %></option>
删除:
<option data-tokens="Select option">Select option</option>
答案 1 :(得分:0)
如果您想知道为什么代码无法正常工作,请在几种情况下关注
window.onload = function() {
var DropdownList = document.getElementById('vehicle');
var SelectedIndex = DropdownList.selectedIndex;
var SelectedValue = DropdownList.value;
var SelectedText = DropdownList.options[DropdownList.selectedIndex].text;
if (SelectedText == null) { // this might actually not work as you think, in case you might need to use `"null"`
// DropDownList.SelectedValue == Select option;
DropDownList.value = "Select option";
} else {
// otherwise display session value
}
}
关于
<input type="tel" id="tel" value="<%=session.getAttribute("mob1") %>" placeholder="Enter number" required="required" />
您尝试过
<input type="tel" id="tel" value="<%=session.getAttribute("mob1") != null ? session.getAttribute("mob1") : "" %>" placeholder="Enter number" required="required" />