2016-09-20 38 views
0

我得到这个JavaScript的错误,我需要做的是动态添加下拉列表中的onchange行。即每次点击“添加更多”按钮,根据下拉菜单的变化添加行并获得文本框的值。JavaScript:Uncaught SyntaxError:missing)在使用onchange事件添加行时参数列表

 $(document).ready(function() { 
      var a = 1; 
      $('.tab_new').on('change', '.product_name', function() { 
       var product_code = $('#product_name' + a).val(); 
       $.ajax({ 
        type: "get", 
        url: 'price.php', 
        data: { 
         product_code: product_code 
        }, 
        dataType: 'json', 
        success: function(data) { 
         if (data) { 
          $('#product_price' + a).val(data) 
         } 
        } 
       }); 
      }); 
      $("#add").click(function() { 
       a = a + 1; 
       $('#datatables tr').eq(-2).after('<tr><td><input type="text" name="client_name[]" value="" id="client_name' + a + '"/></td><td ><select class="product_name" name="product_name[]" id="product_name' + a + '" ><option value=""> --------all-------- </option> <?php $result=mysql_query("select * from product_form"); while($row = mysql_fetch_row($result)) { echo "<option value=' 
        $row[0] 
        '> $row[1] </option>"; } ?></select></td><td ><input type="number" name="product_price[]" value="" id="product_price' + a + '" class=".product_price"/></td></tr>'); 
      }); 
     }); 
HTML: 

<table id="dataTables" class="tab_new"> 
       <thead > 
        <tr> 
         <th>CLIENT NAME</th> 
         <th>PRODUCT NAME </th> 
         <th>PRICE </th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr> 
         <td><input type="text" name="client_name[]" value="" id="client_name"/></td> 
         <td><select name="product_name[]" id="product_name1" class="product_name"> 
           <option value=""> --------ALL-------- </option> 
           <?php $result=mysql_query("Select * from product_form"); 
            while($row = mysql_fetch_row($result)) 
            { 
              echo "<option value='$row[0]'> $row[1] </option>"; 
           } ?> 
         </select></td> 
         <td><input type="number" name="product_price[]" value="" id="product_price1" class="product_price"/></td> 
        </tr> 
       </tbody> 
      </table> 
      <br><br><br> 
      <input type="submit" name="submit" value="Save"> 
      <input type="button" id="add" value="Add More" /> 

I am getting an error missing argument when i clicked on the button . 
+0

还提供HTML代码 –

+1

你试图通过你的JS代码打印PHP解决上述错误。为什么? 另外,$行在JS范围中没有定义,但在这里用作变量。 –

+0

您的PHP在该字符串的中间输出换行符吗? – nnnnnn

回答

0
<script type="text/javascript"> 
      $(document).ready(function(){ 
       var a=1; 
       $('.tab_new').on('change','.product_name', function(){ 
        var product_code = $('#product_name'+a).val(); 
        $.ajax({ 
         type: "get", 
         url: 'price.php', 
         data: {product_code:product_code}, 
         dataType:'json', 
         success: function(data){ 
          if(data){ 
           $('#product_price'+a).val(data) 
          } 
         } 
        }); 
       }); 
       $("#add").click(function(){ 
        a=a+1; 
        $('#dataTables tr').last().after('<tr><td><input type="text" name="client_name[]" value="" id="client_name'+a+'"/></td><td ><select name="product_name[]" id="product_name'+a+'" class="product_name"><option value=""> --------ALL-------- </option> <?php $result=mysql_query("Select * from product_form");while($row = mysql_fetch_row($result)){ ?> <option value="<?php echo $row[0]; ?>"><?php echo $row[1]; ?> </option> <?php } ?></select></td><td ><input type="number" name="product_price[]" value="" id="product_price'+a+'" class="product_price"/></td></tr>'); 
       }); 
      }); 
     </script> 
+0

不要忘记添加js文件:jquery.min.js –

0

你忘了在你的代码中跳过$row[0]

虽然我不明白你为什么试图用JS打印PHP代码。它不会工作。但是,您可以通过转义$row[0]

$(document).ready(function() { 
    var a = 1; 
    $('.tab_new').on('change', '.product_name', function() { 
     var product_code = $('#product_name' + a).val(); 
     $.ajax({ 
     type: "get", 
     url: 'price.php', 
     data: { 
      product_code: product_code 
     }, 
     dataType: 'json', 
     success: function(data) { 
      if (data) { 
      $('#product_price' + a).val(data) 
      } 
     } 
     }); 
    }); 
    $("#add").click(function() { 
     a = a + 1; 
     $('#datatables tr').eq(-2).after('<tr><td><input type="text" name="client_name[]" value="" id="client_name' + a + '"/></td><td ><select class="product_name" name="product_name[]" id="product_name' + a + '" ><option value=""> --------all-------- </option> <?php $result=mysql_query("select * from product_form"); while($row = mysql_fetch_row($result)) { echo "<option value=\'$row[0]\'> $row[1] </option>"; } ?></select></td><td ><input type="number" name="product_price[]" value="" id="product_price' + a + '" class=".product_price"/></td></tr>'); 
    }); 
    }); 
+0

其实我需要的是点击按钮时添加行,也想在改变下拉列表的同时获取文本框的值。 –

+0

​​此代码完全包含在HTML部分中。我只是将它应用到脚本。我已经尝试在codeigniter中用JS打印PHP代码。其工作成功。但不能在这里申请。 –

+0

它不会像这样工作。 你不能通过JS注入来运行php代码。 –

0
price.php file in core php 

     <?php include('connect.php'); 
     if(isset($_REQUEST['product_code'])){ 
      $sql = mysql_query("select * from product_form where product_code =".$_REQUEST['product_code']); 
      $res = mysql_fetch_array($sql); 
      echo $res['product_price'];die; 
     } 
     ?> 

     In case of codeigniter, use this code in controller section 

     public function get_product() 
      { 
       $pro= $this->input->get('product_code'); 
       $procode=$this->form1_model->get_productcode($pro); 
       echo json_encode($procode); 
      } 
相关问题