简体   繁体   English

如何使用jquery更改连接的值以选择动态形式的更改?

[英]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. 单击按钮添加时,用户可以添加具有相同元素的新行,更改选择选项后,我正在使用ajax从数据库获取价格并将其显示在文本输入中。

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+)?" “指定的值” NaN“不是有效数字。该值必须与以下正则表达式匹配:-?(\\ 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 我该如何解决这个问题,或者还有另一种使用行ID更改输入值的方法

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

<script>

function get_price(sel, item) 
{
    $.ajax({
        type: "POST",
        url : "getData.php",
        data: {itemCode: sel },
        success: function(data) {
            $(item).find("input").val(data.price) 
        },
    }); 
}

$(document).ready(function(){
    //add new row when add buttomn clicked 
    var i = 1;

    $("#add_row").click(function(){
        var a = i-1;

        $('#row' + i).html( $('#row' + a).html() )
                   .find('td:first-child')
                   .html(i+1);
        $('#tab_logic').append('<tr id="row' + (i+1) + '"></tr>');
        i++; 
    });

    //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);
    });

});

</script>

我终于解决了,问题是第一次更改一次只调用一次get_price,所以我通过调用onclick函数使用这种方式解决了它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM