使用htmx 提交表單,hx-post 使用操作的目前路徑,同時用操作屬性取代hx-post 我得到正確的路徑。
https://imgur.com/a/Vh3snub
#這不會發送到正確的路徑:
<form hx-post="/signup" class="form" id="sign-up-form" name="signupform" hx-trigger="submit" hx-encoding="multipart/form-data" hx-swap="none" hx-target="this"> <div class="p-4"> <h5>Sign Up</h5> <label>Username:</label> <input class="form-control" autocomplete="username" type="text" name="usernamesignup" placeholder="Username" oninput="checkForValidSignUpSubmission()" required id="usernamesignupinput" /> </div> <div class="p-4"> <label>Email:</label> <input class="form-control" type="email" name="emailsignup" autocomplete="email" placeholder="[email protected]" oninput="checkForValidSignUpSubmission()" required id="emailsignupinput" maxlength="64" /> </div> <div class="p-4"> <label>Password:</label> <input class="form-control" type="password" name="passwordsignup" autocomplete="new-password" placeholder="Password" oninput="checkForValidSignUpSubmission()" required id="passwordsignupinput" /> </div> <div class="p-4"> <label>Confirm Password:</label> <input class="form-control" type="password" autocomplete="new-password" placeholder="Password" oninput="checkForValidSignUpSubmission()" required id="confirmpasswordsignupinput" name="confirmpasswordsignup" /> </div> <div class="p-4"> <label>Pick a profile picture</label> <input id="pfpimage" type="file" accept="image/*" name="pfpformfile" required /> </div> </form>
這確實傳送到正確的路徑:
<form action="/signup" class="form" id="sign-up-form" name="signupform" hx-trigger="submit" hx-encoding="multipart/form-data" hx-swap="none" hx-target="this"> <div class="p-4"> <h5>Sign Up</h5> <label>Username:</label> <input class="form-control" autocomplete="username" type="text" name="usernamesignup" placeholder="Username" oninput="checkForValidSignUpSubmission()" required id="usernamesignupinput" /> </div> <div class="p-4"> <label>Email:</label> <input class="form-control" type="email" name="emailsignup" autocomplete="email" placeholder="[email protected]" oninput="checkForValidSignUpSubmission()" required id="emailsignupinput" maxlength="64" /> </div> <div class="p-4"> <label>Password:</label> <input class="form-control" type="password" name="passwordsignup" autocomplete="new-password" placeholder="Password" oninput="checkForValidSignUpSubmission()" required id="passwordsignupinput" /> </div> <div class="p-4"> <label>Confirm Password:</label> <input class="form-control" type="password" autocomplete="new-password" placeholder="Password" oninput="checkForValidSignUpSubmission()" required id="confirmpasswordsignupinput" name="confirmpasswordsignup" /> </div> <div class="p-4"> <label>Pick a profile picture</label> <input id="pfpimage" type="file" accept="image/*" name="pfpformfile" required /> </div> </form>
我動態添加了提交按鈕,該按鈕在這兩種情況下都有效,因此我沒有費心將其添加到帖子中的程式碼中。
在登入和註冊之間切換時,我從父 div 中刪除了表單。由於某種原因,這與 htmx 屬性混淆了。相反,我將表單的顯示在「無」和「繼承」之間切換,以保持功能完好
以上是hx-post 未使用正確的操作(路徑)的詳細內容。更多資訊請關注PHP中文網其他相關文章!