dongyun7897 2014-10-14 04:56
浏览 42
已采纳

当尝试添加更多字段并选择其冲突第一行时

When i am trying to select option value form row one there's no problem but if i add more and select optional value in second row then its getting conflict first. Every time when you select optional value then only first row conflict i want first row change while changing first select option . Second row select change only second row values.

index.php

<?php
if(!empty($_POST["save"])) {
        $conn = mysql_connect("localhost","root","");
    mysql_select_db("ajaxphp",$conn);
        $itemCount  =   count($_POST["item_name"]);
        $itemValues =   0;
        $query      =   "INSERT INTO item (item_name,item_price) VALUES ";
        $queryValue =   "";
    for($i=0;$i<$itemCount;$i++) {
        if(!empty($_POST["item_name"][$i]) || !empty($_POST["item_price"][$i])) {
            $itemValues++;
            if($queryValue!="") {
                $queryValue .= ",";
            }
            $queryValue .= "('" . $_POST["item_name"][$i] . "', '" . $_POST["item_price"][$i] . "')";
        }
    }
    $sql = $query.$queryValue;
    if($itemValues!=0) {
        $result = mysql_query($sql);
        if(!empty($result)) $message = "Added Successfully.";
    }
}
?>
<HTML>
<HEAD>
<TITLE>PHP jQuery Dynamic Textbox</TITLE>
<LINK href="style.css" rel="stylesheet" type="text/css" />
<SCRIPT src="http://code.jquery.com/jquery-2.1.1.js"></SCRIPT>
<SCRIPT>
function addMore() {
    $("<DIV>").load("input.php", function() {
        $("#product").append($(this).html());
    }); 
}
function deleteRow() {
    $('DIV.product-item').each(function(index, item){
        jQuery(':checkbox', this).each(function () {
            if ($(this).is(':checked')) {
                $(item).remove();
            }
        });
    });
}
</SCRIPT>
</HEAD>
<BODY>
    <FORM name="frmProduct" method="post" action="">
        <DIV id="outer">
            <DIV id="header">
                <DIV class="float-left">&nbsp;</DIV>
                <DIV class="float-left col-heading">Item Name</DIV>
                <DIV class="float-left col-heading">Item Price</DIV>
            </DIV>
            <DIV id="product">
                <?php require_once("input.php") ?>
            </DIV>
            <DIV class="btn-action float-clear">
                <input type="button" name="add_item" value="Add More" onClick="addMore();" />
                <input type="button" name="del_item" value="Delete" onClick="deleteRow();" />
                <span class="success"><?php if(isset($message)) { echo $message; }?></span>
            </DIV>
            <DIV class="footer">
                <input type="submit" name="save" value="Save" />
            </DIV>
        </DIV>
    </form>
</BODY>
</HTML>

input.php

<script type="text/javascript" src="js/jquery.min.js"></script>
<script>
function salesdetail(item_index)
{   
    alert(item_index);

    $.ajax({

        url: 'getsaleinfo.php',
        type: 'POST',
        data: {item_index:item_index},`
        success:function(result){
                alert(result);
                $('#div1').html(result);
        }           
    });
}
</script>

<DIV class="product-item float-clear" style="clear:both;">
    <DIV class="float-left"><input type="checkbox" name="item_index[]" /></DIV>
    <DIV class="float-left"><select name="item_index" id="item_index"  class="required input-small" onchange="salesdetail(this.value);" >
        <option>Select</option>
        <?php
            $conn   =   mysql_connect("localhost","root","");
            mysql_select_db("ajaxphp",$conn);
            $result =   mysql_query("select * from item");
            while($row=mysql_fetch_assoc($result))
            {
                echo "<option>".$row['item_name']."</option>";
            }         
        ?>
        </select>
    </DIV>
    <DIV class="float-left" id="div1"><input type="text" id="unit_price" name="unit_price" /></DIV>
</DIV>

and getsaleinfo.php

<?php
    $conn    =  mysql_connect("localhost","root","");
    mysql_select_db("ajaxphp",$conn);
    $supplier=  $_POST['item_index'];
    $sql     =  "select * from item where item_name='$supplier'";
    $rs      =  mysql_query($sql);
?>
<?php
if($row = mysql_fetch_array($rs)) {
?>
    <div class="float-left">
        <!--<label id="unit" ></label>-->
        <input  type="text"  name="unit_price" id="unit_price" class="input-mini" value="<?php echo $row['item_price'];?>" >
    </div>
<?php   }
?>  

database

CREATE TABLE IF NOT EXISTS `item` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `item_name` varchar(255) NOT NULL,
  `item_price` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

INSERT INTO `item` (`id`, `item_name`, `item_price`) VALUES
(1, 'hello', 21),
(2, 'hi', 22);
  • 写回答

3条回答 默认 最新

  • dongyue4964 2014-10-14 06:06
    关注

    try this...

            $('body').on('change','#item_index',function() {    //works for ajax loaded contents
            var id      =   $("#item_index").val();
    
            var formid  =   new FormData();
    
            formid.append('item_index',id);
    
            $.ajax({            
    
                            url         :   'getsaleinfo.php',
                            dataType    :   'text',
                            cache       :   false,
                            contentType :   false,
                            processData :   false,
                            data        :   formid,        
                            type        :   'post',
                            success     :   function(data){     
                                        alert(result);
                                        $('#div1').html(result);                        
                                        //document.getElementById("div1").innerHTML=data;
    
                            }
                    });
         }
    

    insted of onchange call this will do...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题