I would like to ask you how to keep the selected value even if it is reloaded.

Asked 2 years ago, Updated 2 years ago, 47 views

Hello,

<select id="test" name="num" onchange="this.form.submit()">
  <option value=""> ----------------------------  Not Selected  ---------------------------- </option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

If you select the HTML select value with the above code, I made it to submit to django without a button, but the value is initialized to Not Selected after submission, is there any way to keep the value selected? I have a question.

html django

2022-09-21 10:19

1 Answers

Whether the form submit method is GET or POST is unknown on the attached code, but

If submit should occur every time the select option is changed, do not call form.submit() from the change event handler in the select element and write your own Ajax call.

https://codepen.io/digda/pen/ZEbLaXE

However, this will force cumbersome work.

In the case of using the existing form submission, the contents in the form are automatically sent in a form suitable for the method, whereas

If you make Ajax and call it directly, you have to collect the data to be shipped one by one and put it in manually.


2022-09-21 10:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.