简体   繁体   中英

Add Action to Form in HTML using Jquery

I am attempting to add the action attribute to my form element, on the load of a jquery function. However, I seem to be missing something. I have tried searching for this issue, and I have not found any luck in regards to my issue. Here is the HTML in question:

 <form method="post" enctype="multipart/form-data" id="#photouploader">
            Upload Client Photo Here
            <input type="file" name="thumbnail">
            <input type="submit" value="Upload Image">
          </form>

This is the Jquery function I wrote:

 $(document).ready(function(){
 var id = document.location.href.split('client/')[1]; 
 $('#photouploader').attr('action', '/form-upload/'+id);
 });

I am just looking to understand where I am going wrong here.

You have a # in your HTML form ID - Remove it and your jQuery should work. Should look like this:

<form method="post" enctype="multipart/form-data" id="photouploader">

The # in jquery is not part of the ID, it just indicates that the following text is referencing an element ID, similar to how . indicates class.

EDIT: Credit to eatpeanutbutter , he had the correct answer in the comments originally. I was typing mine up as it was posted.

Your selector $('#photouploader').attr('action', '/form-upload/'+id); is looking for an id called photouploader . You do not have an HTML element with an ID of photouploader . Instead, you have an element with an ID of #photouploader .

Simply remove the # from your ID attribute in the HTML and you should be good to go -

<form method="post" enctype="multipart/form-data" id="photouploader">

Remove # from id (id="photouploader">) and try this:

$(document).ready(function(){
 var id = document.location.href.split('client/')[1]; 
 $('#photouploader').attr('action', '/form-upload/'+id);
});

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