我需要在提交表单之前检查输入字段上的值是否已更改。这是我到目前为止的代码:
JS
$(document).ready(function(){
$('#submitButton').click(function() {
if( $('input[name="inputToBeChecked"]').val() != 'Original input field value' {
alert("Input field has changed!");
} else {
alert("Input field has not changed!");
}
});
});
HTML
<form>
<input type="text" name="inputToBeChecked" id="inputToBeChecked" value="Original input field value">
<a id="submitButton" type="button" class="btn" href="javascript:void(0);">Submit form</a>
</form>
答案 0 :(得分:3)
只需在输入更改后设置标记
var flag = 0;
$('input[name="inputToBeChecked"]').change(function(){
flag = 1;
});
$('#submitButton').click(function() {
if(flag == 1){
//yeah!
}
});
还有另一种情况,如果它被改变然后返回到初始状态。然后你可以改为保存初始值。
var initialVal;
$(document).ready(function(){
initialVal = $('input[name="inputToBeChecked"]').val();
});
$('#submitButton').click(function() {
if($('input[name="inputToBeChecked"]').val() != initialVal){
// initial value changed
} else {
// initial value either unchanged or changed and later reversed to initial state
}
});