简体   繁体   中英

Jquery Fill in Form Input Text Value

On page load I would like jquery to load the UUID method as a value to the UUID field so that I see it passed as a parameter on submit. This and this SO post helped, but Im still not getting there...

<FORM ACTION="urlAuction.html" METHOD=GET>
 <INPUT TYPE=TEXT NAME="url">
 <INPUT id="uuid" TYPE=TEXT NAME="uuid" VALUE="" >
 <INPUT TYPE=SUBMIT VALUE=Submit>
</FORM>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript"> 
 $(document).ready(function() {
     var $uuid = $('#uuid'); 
         $uuid.val(GUID());
 });
 function GUID ()
 {
  'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
      var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
      return v.toString(16);
 });
}
</script>

Your GUID function lacks a return to return the value it produces for you:

 function GUID ()
 {
     return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c)
     {
        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
        return v.toString(16);
     });
 }

It looks like it returns something the way you formatted the braces, but in fact the return v.toString(16) belongs to the anonymous callback function being used by replace to populate each digit with a random hexdecimal digit.

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