jQuery根据选择值和多行(父函数?)填充表中的文本输入

时间:2014-01-12 15:38:32

标签: javascript php jquery mysql ajax

案例
我已经制作了一个表格,你可以用文章订购。这些文章以另一种形式插入表格中。在这些表中,文章与文章代码,描述和价格一起保存。

您的想法是在表单中选择一篇文章来制作订单。如果您选择文章(带有选择输入),价格将自动填入“价格”输入字段。

问题
目前我的问题是,当您在一行中选择一篇文章时,该脚本会在所有其他行中填写相关的文章价格,而不是仅在选择输入所在的行中填写。 我想我必须对我使用的Ajax代码中的partent函数做一些事情,但我不知道该怎么做。

有人能给我一些建议吗?

我使用以下Ajax / jQuery脚本:

<script type="text/javascript">
    jQuery(document).ready(function(){
        var id = $(this).parent();
        jQuery('#Article_ID').live('change', function(event) {
            $.ajax({
                url     : 'getData.php',
                type    : 'POST',
                dataType: 'json',
                data    : $('#myForm').serialize(),
                success: function( data ) {
                       for(var id in data) {        
                              $(id).val( data[id] );
                       }
                }
            });
        });
    });
</script>

getData文件包含以下代码:

$clientId = "-1";
if (isset($_POST['Article_ID'])) {
  $clientId = $_POST['Article_ID'];
}
mysql_select_db($database_connDatabase, $connDatabase);
$query_Recordset1 = sprintf("SELECT * FROM articles WHERE id = %s", GetSQLValueString($Article_ID, "int"));
$Recordset1 = mysql_query($query_Recordset1, $connDatabase) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$add1 = $row_Recordset1['description'];
$add2 = $row_Recordset1['price'];

$arr = array( 'input#description' => $add1, 'input#price' => $add2 );
echo json_encode( $arr );

mysql_free_result($Recordset1);

表单包含以下规则代码:

<script type="text/javascript">
var count = 0;
$(function(){
    $('p#add_field').click(function(){
        count += 1;
        $('#container').append('<div class="regel"><select id="Article_ID" name="Article_ID" style="width:17px; height:30px; border:none;margin-right:0px;"><option value="">Select</option><option value="1">1</option><option value="2">2</option></select><input id="description" name="description[]' + '" type="text" class="field-l" style="width:425px"/><input id="price" name="price[]' + '" type="text" value="0.00" class="field-l" style="width:75px; margin-left:6px; text-align:right"/></p>' );  
    });
});
</script>

1 个答案:

答案 0 :(得分:0)

  

the script fills in the associated article price in all the other rows instead of only in the row where the select input is in

这是因为您添加了具有相同ID的新行onClick<div class="regel">..</div>):Article_IDdescriptionprice。属性ID必须是唯一的。

尝试。 接下来改变(PHP):

$arr = array( 'input#description' => $add1, 'input#price' => $add2 );
echo json_encode( $arr );

为:

$arr = array( 'description' => $add1, 'price' => $add2 );
echo json_encode( $arr );
die;

更改JS

    jQuery('#Article_ID').live('change', function(event) {
        var parent = $(this).parent(); // current `<div class="regel">`
        $.ajax({
            url     : 'getData.php',
            type    : 'POST',
            dataType: 'json',
            data    : $('#myForm').serialize(),
            success: function( data ) {
                $(parent+" #description").val(data.description); // 
                $(parent+" #price").val(data.price);
            }
        });
    });