私は、次のjQueryのスクリプトを持っている:このJqueryスクリプトを簡略化できますか?
$(document).ready(function() {
// When the submit button of the new team form is clicked...
$('#new_team').submit(function() {
// Disable & rename the save button
$('input#save_button', this).attr('disabled', 'disabled').val('Saving...');
// Fade in the saving message
$("#saving-msg").fadeIn(200);
});
// Perform the AJAX request to the server
$('#new_team').ajaxForm({
url: "/teams/create.json",
type: "POST",
dataType: "json",
beforeSubmit: function(formData, jqForm, options) {},
success: function(response) {
if(response.success == true) {
// If no errors then redirect
window.location = "/teams/" + response.team_id;
} else {
// Enable and rename the save button
$('input#save_button').removeAttr('disabled').val('Save');
// Output errors
$("#error_messages").html(errorMessages(response)).hide().fadeIn(300);
// Fade out save message
$("#saving-msg").fadeOut(200);
// Scroll to the top
window.scrollTo(0,0);
}
}
}); });
は、このスクリプトを簡素化し、また、それがさまざまな形態のために再利用可能にするとにかくはありますか?
- ボタンは常に同じ
- 各フォームにラベル付けされているページにのみ、これまで一つの形態があるが、固有IDに
感謝しています!
これは本当に良い方法ではありませんが、フォームが1つしかないので、セレクタ$( 'form')を使用してフォームを参照できます。次に、$(this).find( 'input#save_button')を使用できるように、そのフォームの子としてボタンを参照します。 – user120242