在jsp中,是否有任何属性可提供下拉列表中的值已更改的信息?
举例说明:
我有下拉菜单国家-在页面加载中,下拉菜单中有“美国”,我要更改为“英国”,然后再次更改为“美国”。在这种情况下,我没有做任何更改,所以没有什么可保存的,但是如果单击“保存”按钮,则页面将被提交。无论如何,要避免提交此页面。
最佳答案
是的。但是您必须使用javascript
,而不是jsp
。范例程式码
的HTML:
<form class="checked">
<input type="hidden" id="countrySelectInitial" value="UK" /> <!-- we need this element to store initial value -->
<select name="country" class="checkedInput" id="countrySelect">
<option value="US">US</option>
<option value="UK" selected>UK</option>
</select>
<input type="submit" class="submitButton" disabled />
</form>
js:
$(".checkedInput").change(function() {
var newValue = $(this).val();
var idValue = $(this).attr('id'); // we need id to retrieve initial value. Initial value is stored in element with id <id>Initial
var oldValue = $("#" + idValue + "Initial").val();
if(newValue === oldValue) {
// if value don't changed - disable button. user have no possibility to submit form
$(this).parents("form.checked").find(".submitButton").attr("disabled", true);
} else {
// if value changed - remove "disabled" attribute. User can click button and submit form
$(this).parents("form".checked).find(".submitButton").removeAttr("disabled");
}
});