Я попытался написать функцию, которая происходит во всех формах на странице:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$('form').click(function() {
alert (this.serialize());
return false;
});
</script>
но я получаю ошибку
this.serialize не является функцией
Что я делаю не так?
$(this)
Итак, теперь я попробовал:
$('form').click(function() {
alert ($(this).serialize());
$("input, select, textarea").attr("disabled", true);
alert ($(this).serialize());
return false;
});
Но это отключает все элементы во всех формах на странице.
Как я могу просто отключить тех, кто был нажат кнопку submitbutton?
Попробуйте
$(this).serialize();
Вместо
this.serialize();
Чтобы отключить только поля этой формы,
$('form').click(function () {
// ...
$(this).find('input, select, textarea').each(function () {
$(this).prop('disabled', true);
});
// ...
});
$()
- это функция конструктора jQuery, а this
ссылка на элемент вызова DOM. Поэтому всякий раз, когда вам нужна функция jQuery, окружайте this
$()
.
Я отвечаю на ваш второй вопрос об инвалидах.
Если вы хотите, вы можете сделать что-то вроде:
$('#saveButtonForm3').click(function() {
alert ($(this).parent().serialize()); // parent() is the form, $(this) is the button
// this line will disable the fields in the form where the save button is clicked
$(this).parent().find("input, select, textarea").attr("disabled", true);
alert ($(this).parent().serialize());
return false;
});
Вам нужно использовать объект jQuery: $(this).serialize()
$(this).find("input, select, textarea").attr(...)