Reputation: 3721
I have a select type
dropdown, which upon selection redirects users to another page. I want them to be warned if they try to navigate away without saving when using this select
dropdown.
However it needs to ignore this specific select type
as one of the input types
when determining that an input type has changed. This is my script, but it doesn't perform the desired action, (where .send1
references to the actual select type
):
$(document).on('change', '.send1', function(){
if($(":input").not('.send1')){
$(":input").change(function(){
unsaved = true;
});
}
});
If unsaved
== true then a users have a warning that there are unsaved changes.
Upvotes: 0
Views: 76
Reputation: 144679
.not()
method returns a jQuery object, and an object is considered a truthy value in JavaScript, you should use the length
property for checking the length of the collection. But here this is not the main problem, the whole logic doesn't sound promising. If you want to exclude an element from the selection you can use the :not
in your selector:
var unsaved = false;
$(document).on('change', '.send1:not("select.specific")', function() {
unsaved = true;
});
$('select.specific').on('change', function() {
if (unsaved) {
// show a modal/alert something...
} else {
// redirect
}
});
Upvotes: 1
Reputation: 3034
I don't really understand what you're asking. It seems you're trying to determine if an input control has the class 'send1' - and if so, you ignore it? Why are you binding all of your inputs to your event handler? Can you post your HTML markup?
The closest I can come to helping is to recommend that you put a class on all of your inputs that COULD have unsaved data in them. Then, write an event handler for your select list that redirects to another page:
$("#ddlThatRedirects").change(function(e){
if (CheckFields()){
//Put whatever alerting you want here
}
else{
//Put redirect or postback logic here
}
});
function CheckFields(){
var UnsavedChanges = false;
$(".ClassNameOfInputs").each(function(){
if ($(this).val().length > 0)
UnsavedChanges = true;
});
return UnsavedChanges;
}
Upvotes: 0