Problmemem jest to ze submit nie odpala funkcji handleSubmit <div…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Problmemem jest to ze submit nie odpala funkcji handleSubmit <form id="external-search-form" ref="form" @submit.prevent="handleSubmit(event)" @input="handleLocationInput" autocomplete="off" />
<!-- Autocomplete dropdown -->
<div v-if="autocomplete.visible.location" class="autocomplete-results shadow border">
<div v-if="autocomplete.loading.location" class="autocomplete-loading p-2 text-center">
<div class="spinner-border spinner-border-sm" role="status">
<span class="sr-only">Loading...</span>
</div>
</div>
<div v-else-if="autocomplete.location.length === 0" class="p-2 text-center text-muted">
No results
</div>
<div v-else class="autocomplete-items">
<div v-for="(item, index) in autocomplete.location"
:key="index"
:class="{'active': index === autocomplete.selected.location}"
class="autocomplete-item p-2"
@mousedown="selectAutocompleteItem('location', item)">
{{ item.name }}
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-2 col-md-3 col-sm-12">
<button type="submit">FIND JOB <i class="la la-search"></i></button>
</div>
</div>
</form>
</div><!-- Job Search 2 -->
//assets/block/search-block/external-search-form.js
(function () { const { createApp } = Vue;
// Wait for DOM to be ready
document.addEventListener('DOMContentLoaded', function () {
const externalSearchForm = document.querySelector('#external-search-form');
if (!externalSearchForm) return;
const app = createApp({ data() { return { fields: { s: '', location: '', city: '', province: '' }, autocomplete: { visible: { location: false }, location: [], selected: { location: -1 }, loading: { location: false } }, searchbar: { locations: [] } } }, async mounted() { await this.loadLocations(); }, methods: { async loadLocations() { const formData = { first: true, showblocks: false, gpsWork: false, searchToShow: false, _wpnonce: searchJob._wpnonce };
})();
The issue you're describing — the handleSubmit function not being triggered when the form is submitted — can be caused by several factors. Let's analyze the code and identify potential problems.
The form submission handler is defined as:
<form id="external-search-form" ref="form" @submit.prevent="handleSubmit($event)" v-cloak>
Here, the @submit.prevent directive should prevent the default form submission behavior and call the handleSubmit method. However, if handleSubmit is not being triggered, it could indicate:
#external-search-form).@submit.prevent directive is recognized by Vue.Add a console.log statement in the mounted lifecycle hook to confirm that the Vue app is mounted successfully:
async mounted() {
console.log('Vue app mounted');
await this.loadLocations();
},
If this log does not appear in the browser console, the Vue app is not being mounted correctly. Double-check that:
#external-search-form element exists in the DOM when the script runs.The handleSubmit method is defined as:
handleSubmit(event) {
debugger;
// event.preventDefault();
// const searchParams = new URLSearchParams();
// if (this.fields.s.trim()) {
// searchParams.append('s', this.fields.s.trim());
// }
// if (this.fields.city) {
// searchParams.append('city', this.fields.city);
// }
// if (this.fields.province) {
// searchParams.append('province', this.fields.province);
// }
// const searchUrl = '/oferty-pracy/?' + searchParams.toString();
// window.location.href = searchUrl;
}
try {
const response = await $.ajax({
url: '/wp-json/st/v1/search_job',
type: 'POST',
dataType: 'json',
data: { formData: formData }
});
if (response.success) {
this.searchbar.locations = response.data.locations || [];
}
} catch (error) {
console.error('Error loading locations:', error);
}
},
showAutocomplete(field) {
if (field === 'location') {
this.autocomplete.visible.location = true;
this.autocomplete.selected.location = -1;
this.filterLocations();
}
},
hideAutocomplete(field) {
setTimeout(() => {
this.autocomplete.visible[field] = false;
}, 200);
},
filterLocations() {
const query = this.fields.location.toLowerCase();
this.autocomplete.location = this.searchbar.locations.filter(location =>
location.name.toLowerCase().includes(query)
).slice(0, 10);
},
handleKeyDown(field, event) {
if (field === 'location' && this.autocomplete.visible.location) {
const items = this.autocomplete.location;
if (event.key === 'ArrowDown') {
event.preventDefault();
this.autocomplete.selected.location =
this.autocomplete.selected.location < items.length - 1
? this.autocomplete.selected.location + 1
: 0;
} else if (event.key === 'ArrowUp') {
event.preventDefault();
this.autocomplete.selected.location =
this.autocomplete.selected.location > 0
? this.autocomplete.selected.location - 1
: items.length - 1;
} else if (event.key === 'Enter') {
event.preventDefault();
if (this.autocomplete.selected.location >= 0) {
this.selectAutocompleteItem('location', items[this.autocomplete.selected.location]);
}
} else if (event.key === 'Escape') {
this.autocomplete.visible.location = false;
}
}
},
selectAutocompleteItem(field, item) {
if (field === 'location') {
this.fields.location = item.name;
this.fields.city = item.city;
this.fields.province = item.province;
this.autocomplete.visible.location = false;
}
},
handleLocationInput() {
this.filterLocations();
// Clear city and province if user types manually
this.fields.city = '';
this.fields.province = '';
},
handleSubmit(event) {
debugger
// event.preventDefault();
// const searchParams = new URLSearchParams();
// if (this.fields.s.trim()) {
// searchParams.append('s', this.fields.s.trim());
// }
// if (this.fields.city) {
// searchParams.append('city', this.fields.city);
// }
// if (this.fields.province) {
// searchParams.append('province', this.fields.province);
// }
// const searchUrl = '/oferty-pracy/?' + searchParams.toString();
// window.location.href = searchUrl;
}
}
});
app.mount('#external-search-form');
});
The debugger statement is present, but there is no actual logic executed in this method. If the browser's developer tools are not open, the debugger statement will have no effect.
Replace the debugger statement with a console.log to confirm that the method is being called:
handleSubmit(event) {
console.log('handleSubmit triggered', event);
// Your existing logic here...
}
If the log does not appear in the console, the method is not being invoked. This could indicate an issue with the Vue event binding or the DOM structure.
The submit button is defined as:
<button type="submit">FIND JOB <i class="la la-search"></i></button>
By default, a <button> inside a <form> has type="submit". However, if another script modifies the button's type attribute (e.g., setting it to button), the form submission will not trigger.
Ensure the button explicitly has type="submit":
<button type="submit">FIND JOB <i class="la la-search"></i></button>
If other scripts on the page interfere with the form or its elements, they might prevent the handleSubmit method from being called. For example:
onsubmit handler.The code uses Vue 3 (createApp), but if the project includes Vue 2 components or libraries, there could be compatibility issues.
Ensure that the correct version of Vue is loaded and that there are no conflicts between Vue 2 and Vue 3.
v-model BindingsThe form fields use v-model bindings:
<input type="text" placeholder="Keywords" v-model="fields.s" />
<input type="text" placeholder="Location" v-model="fields.location" />
If the fields object is not properly initialized or updated, Vue might fail to bind the inputs correctly.
v-model BindingsAdd logging to verify that the fields object is updated as expected:
methods: {
handleLocationInput() {
console.log('Location input:', this.fields.location);
this.filterLocations();
this.fields.city = '';
this.fields.province = '';
}
}
v-cloak IssueThe v-cloak directive is used to hide uncompiled Vue templates until the app is ready. If the CSS rule for v-cloak is missing or incorrect, the form might not render properly.
v-cloak CSS RuleEnsure the following CSS rule is present:
[v-cloak] {
display: none;
}
handleSubmit method is called.After analyzing the code, the most likely causes are:
handleSubmit method is not being called due to a conflict or misconfiguration.By systematically debugging each component (Vue initialization, event binding, DOM structure, and external scripts), you should be able to identify and resolve the issue.