javascript - How can I change the value that is connected to select change in dynamic form using jquery?


I was trying to make a form, which has two elements, one select item and one input price, select option are stored in database, so I retrieve it and display it.

User can add new row with same elements when button add is clicked, when the select option is changed, I am using ajax to get the price from database and displayed it in the text input.

It all worked fine with the first row but when user add new row I cannot retrieve the price and get the following message

"the specified value "NaN" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)?"

How can I solve this problem or is there is another way to change the value of input using the row id

<table id="tab_logic">
  <tr id='row0'>
      <select name="item[]" required>
        $items = "SELECT itemCode, itemName FROM items";
        $itemslist = $conn->query($items);
        while($row = $itemslist->fetch_assoc()) {
          echo "<option value='" . $row["itemCode"] . "'>" . $row["itemName"]. "</option>" ;
      <input type="text" name='price[]' required/>
  <tr id='row1'></tr>
<button id="add_row" >Add Row</button>


function get_price(sel, item) 
    type: "POST",
    url : "getData.php",
    data: {itemCode: sel },
    success: function(data) {

  //add new row when add buttomn clicked 
  var i = 1;

    var a = i-1;

    $('#row' + i).html( $('#row' + a).html() )
    $('#tab_logic').append('<tr id="row' + (i+1) + '"></tr>');

  //when selected item change get parent and call function 
  $('#tab_logic select').on('change', function() {
    var item = $(this).parent().parent();

    get_price(this.value, item);





I solved it finally, the problem was that get_price is only called one time on first change so I solved it using this way by calling the function onclick

    <select class="form-control item" name="item[]" onclick="get_price(this.value,$(this).parent().parent());" id='1' required>

People are also looking for solutions to the problem: apache - codeigniter htaccess php instructions not working


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.