I am having a problem with the below code. I am not sure why this selector doesn't work, if I click to submit it doesn't work. Does anybody have any idea what would be proper way without using id or class selector?
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
</head>
<body>
<script>
$(this).closest('form').submit(function () {
alert(1);
return false;
});
</script>
<form>
<input type="text" name="example">
<input type="submit" class="searchBtn">
</form>
</body>
</html>
As gdoron were asking me what I am trying to do, I am putting here the entire code, together with Sudhir solution, if anybody else need it. So I am trying to send input values with ajax on submit, but I don't want use class or id selector:
$("input[type='submit']").click(function() {
$(this).closest('form').submit(function() {
var values = $(this).serialize();
$.ajax({
type: "POST",
url: $(this).closest('form').attr('action'),
data: values,
success: function(msg){
//saving done
closeDialog(200);
}
});
return false;
});
});
try:
Change your code:
To:
this
in your code is thewindow
, no anything inside the<form>
.