2013-11-21 101 views
0

无法获取文本框的值无法获取文本框的值

我想下面的代码

$(document).on('focusout', '.barcode', function(event){ 
       event.preventDefault(); 

       var pid=$(this).parents('.row').attr('id'); 
       alert(pid); 
       var bcd=$(pid+' .barcode').val(); 
       alert($(pid+' .barcode')); 
       alert(bcd); 
       var datastr='barCode='+bcd; 
       alert(datastr); 
      }); 

我想它下面的HTML

<tr class="row" id="item10"> 
            <td><input type="text" name="barcode" class="barcode" style="width: 50px"/></td> 
            <td><input type="text" name="type" class="type" style="width: 50px"/></td> 
            <td><input type="text" name="color" class="color" style="width: 50px"/></td> 
            <td><input type="text" name="qty" class="qty" style="width: 50px"/></td> 
            <td><input type="text" name="price" class="price" style="width: 50px"/></td> 

           </tr> 


    value of var `id` is correct but `bcd` and `dataStr` is not correct. 

请看有什么问题

+0

请使用一致的代码缩进代码。 –

回答

3

你缺少“#”id指示灯

var bcd=$("#"+pid+' .barcode').val(); 

var pid="#"+$(this).parents('.row').attr('id'); 

而且<tr>必须<table>包裹柜面你不是。

DEMO

0

而且,你是里面

$(document).ready(function() { 
...... 
}); 
+1

这不是因为他使用$(document).on(' – Anton

+0

好吧,谢谢,我是jquery的新手。 –

2
 $(document).on('focusout', '.barcode', function(event){ 
      event.preventDefault(); 
      alert($(this).val()); 
     }); 

你可以使用$(本).VAL()来获得该元素的值

+0

+1没有想到那个^^我太专注于修复他的代码 – Anton