2013-08-21 19 views
0

在jsp中,是否有任何屬性可以提供下拉列表中的值發生更改的信息?檢測JSP中的下拉改變?

說實例:

我有下拉的國家 - 在頁面加載它有「US」的下拉菜單,我切換到「英國」,然後再切換到「美」。在這種情況下,我沒有做任何改變,所以我沒有什麼可以保存,但如果我點擊保存按鈕我的頁面正在提交。無論如何避免這個頁面提交。?

+0

JSP是一個服務器端語言。我不認爲如果沒有客戶端語言如Javascript,你可以真正做到這一點。 – mattbdean

+0

好的。謝謝你。 – DeepVeen

回答

1

是的。但是您必須使用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"); 
    } 
});