Error with parameter to change event receiving unintended data.

Asked 2 years ago, Updated 2 years ago, 42 views

There is a combo box called payment date on the screen, and the value is set to the value obtained from DB processing.After setting it up, the initial value is to display the value processed by the server side.
Also, when the change event occurs in the payment date combo box, the server side processing value displayed as the initial value of the change event will be crossed, but the change event processing is repeated over and over again.
What do you think is the cause?Ideally, the change event should be carried out over only the server-side-processed value displayed as the initial value as a parameter to the change event.

Example Code:

Payment Date Combo Box.change(function(event){
  // Here, the value acquired by the val method becomes the value acquired from DB processing.

● Processing Flow
·In the initial display of the screen, the value obtained from DB is received by JS using Ajax and set to the payment date combo box, and the value processed by the server side is displayed in the initial display value
·Change event processing of payment date combo box occurred

javascript jquery

2022-09-30 14:42

1 Answers

When the change event occurred, I thought that the value processed by the server side, which was displayed as the initial value of the change event, would be passed, but the value obtained from DB was repeatedly

I don't understand the meaning of server-side processed values and retrieved from DB, but once both values are set to <select></select> in HTML,

In the change function, if you get the value of $("option:selected", this) (text() or val()) you can get the changed value of <option/> as shown below.

$("select").change(function(){
    var str = $("option:selected", this).text()+":"+$("option:selected", this).val()
    $("div").text(str);
})
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

<select name="sweets">
  <option value="1">Chocolate</option>
  <option value="2">Candy</option>
  <option value="3">Taffy</option>
  <option value="4">Caramel</option>
  <option value="5">Fudge</option>
  <option value="6">Cookie</option>
</select>

<h2>Selections</h2>
<div></div> 

Reference: jquery select change event get selected option


2022-09-30 14:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.