How to populate a blank form field with onSubmit
New to JavaScript so please forgive my ignorance.
I have a form where when the customer submits the form it checks a specific field and if it's left blank I need it to populate it with a specific code. Any ideas how I can do this?
<form m开发者_开发技巧ethod="post" action="index.php" name="checkout">
<table width="100%" border="0" cellpadding="5" cellspacing="10">
<tr>
<td align="right" class="normaltext" width="40%">
<b>Catalog Code:</b>
</td>
<td align="left">
<input type="text" name="cat_code" value="{$valid.cat_code}" size="25" class="formtext" />
</td>
</tr>
</table>
Then the submit button:
<input type="submit" name="submit" value="Continue >" class="addtocart_btn btnstyle1" />
I guess you can use jQuery to do something like this:
<form id="checkout" name="checkout" method="post" action="index.php">
<input id="input1" type="text" value="" />
<input id="input2" type="text" value="" />
...
<input id="inputn" type="text" value="" />
</form>
<script type="text/javascript">
$("checkout").submit({
if ($("#input1").val() == "") {
$("#input1").val() = "Your desired value for input1";
}
// put more if condition to populate the blank fields as needed
});
<script>
Add an onsubmit
handler to your form:
<script type="text/javascript">
window.onload = function()
{
document.forms.checkout.onsubmit = checkBlankField;
}
function checkBlankField()
{
var field = document.forms.checkout.cat_code;
if (!field.value)
{
field.value = "some value";
}
}
</script>
精彩评论