简体   繁体   中英

Use return value function in html input field

After user types value in input field, I have an onblur functions that returns the correct layout of the input field (set dashes on the right place). After sending the form, the input field is empty.

How can I set the value to stay after sending the form?

function Licenseplate(sKenteken){

sKenteken=sKenteken.toUpperCase().replace(/[^0-9A-Z]/g,"");
var regexp1=/[A-Z]\d|\d[A-Z]/;
var regexp2=/[A-Z]{4}|\d{4}/;
var aMatches;

while(aMatches=sKenteken.match(regexp1))

sKenteken=sKenteken.replace(new RegExp(aMatches[0]),
aMatches[0].charAt(0)+"-"+aMatches[0].charAt(1))

while(aMatches=sKenteken.match(regexp2))

sKenteken=sKenteken.replace(new RegExp(aMatches[0]),
aMatches[0].charAt(0)+aMatches[0].charAt(1)+"-"+aMatches[0].charAt(2)+aMatches[0].charAt(3))

return sKenteken;}

This is my form.

<form action="/?" method="POST" enctype="application/x-www-form-urlencoded" name="kenteken" id="kentekenForm">
    <input name="kenteken" type="text" value="" size="8" maxlength="8" id="kenteken" onblur="this.value=Licenseplate(this.value)"/>
    <input id="subHere" type="submit" value="Submit" />
  </form>

不要使用replace:

sKenteken=sKenteken.toUpperCase().replace(/[^0-9A-Z]/g,"");

It is normal behavior of a form to be empty after being sent to the server. If you want the form to be filled with the license plate, in your script insert the license plate in the field, after processing the form.

An example in PHP:

<?php

// Process the form
...

// Output the form
$kenteken = ( isset( $_POST['kenteken'] ) ? $_POST['kenteken'] : "";
?>
<form ...>
    <input name="kenteken" type="text" value="<?php echo $kenteken ?>" size="8" maxlength="8" id="kenteken" onblur="this.value=Licenseplate(this.value)"/>
    <input id="subHere" type="submit" value="Submit" />
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM