Maybe script being added after the page has already been loaded, which results in the conditions not working until a refresh. Ensure that the script is included in the page before the form is rendered, or use $(document).ready() or window.onload
$(document).ready()
window.onload