2017-04-19 37 views
1

這裏是代碼我想獲取的jQuery動態單選按鈕字段的值

<table class="table"> 
    <tbody> 
    <tr> 
     <td></td> 
     <td>Destination</td> 
     <td>Service Charges</td> 
     <td>Time Frame</td> 
    </tr> 
    <tr class="uk"> 
     <td><input checked="checked" type="radio" name="shiping-method" id="shipping-method" value="1"></td> 
     <td>uk</td> 
     <td>free</td> 
     <td>2-4 working days</td> 
    </tr> 
    <tr class="uk"> 
     <td><input type="radio" name="shiping-method" id="shipping-method" value="3"></td> 
     <td>uk</td> 
     <td>6.00</td> 
     <td>next working day</td> 
    </tr> 
    </tbody> 
</table> 

這些值不主頁 我嘗試使用下面的代碼

$('input[type="radio"]').on('click',function(){ 
    var value = $(this).val(); 
    alert($(this).val()); 
}); 
+0

使用事件代理如果動態添加的 – guradio

+1

可能的複製[事件綁定上動態創建的元素?](http://stackoverflow.com/questions/203198/event-binding-on-動態創建元素) – guradio

+0

即時通訊使用「開」但它不起作用 – samar

回答

0

上訪問您可以在下面的方式使用event delegation: -

$('.table').on('click','input[type="radio"]',function(){ 
    var value = $(this).val(); 
    alert($(this).val()); 
}); 

工作例如: -

$('.table').on('click','input[type="radio"]',function(){ 
 
     var value = $(this).val(); 
 
    alert($(this).val()); 
 

 
}); 
 

 
$('.clickMe').click(function(){ 
 
$("<tr class='uk'><td><input checked='checked' type='radio' name='shiping-method' id='shipping-method' value='1'></td><td>uk</td><td>free</td><td>2-4 working days</td></tr><tr class='uk'><td><input type='radio' name='shiping-method' id='shipping-method' value='3'></td><td>uk</td><td>6.00</td><td>next working day</td></tr>").insertAfter($('.table tbody tr')); 
 

 
});
table { 
 
    width: 100%; 
 
} 
 

 
tr { 
 
    height: 50px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="table"> 
 
    <tbody> 
 
    <tr> 
 
     <td>Check</td> 
 
     <td>Destination</td> 
 
     <td>Service Charges</td> 
 
     <td>Time Frame</td> 
 
    </tr> 
 
    </tbody> 
 
</table> 
 
<br> 
 
<input type="button" class="clickMe" value="ClickMe!">

+0

薩馬很高興幫助你:) :) –