zwykły DOM (brak biblioteki), twój HTML będzie wyglądać mniej więcej tak:
<form name="foo" action="bar" method="post">
<!-- or method="get" -->
<input name="somefield">
<input type="submit" value="Submit">
</form>
i twój scenariusz będzie wyglądał mniej więcej tak:
var form = document.forms.foo;
if (form && form.elements.something)
// I use onsubmit here for brevity. Really, you want to use a
// function that uses form.attachEvent or form.addEventListener
// based on feature detection.
form.onsubmit = function() {
// if (form.elements.foo.value.trim()) is preferable but trim()
// is not available everywhere. Note that jQuery has $.trim(),
// and most general purpose libraries include a trim(s)
// function.
if (form.elements.something.value.match(/^\s*$/)))
form.elements.something.value = 'DEFAULT VALUE';
};
można wyjaśnić Wskazówka: Komunikat więcej ? Mówisz, że musisz zawinąć całą rzecz w: $ (document) .ready (function() {YOURCODEHERE}); – tgharold
@tgharold Nie, ponieważ nie jest to związane z pytaniem. Całość dokładnie wyjaśnia w dostarczonym łączu. – mekwall