2014-11-15 128 views
2

我需要一些幫助來弄清楚。 我有一個按鈕收音機,當這個按鈕被選中,就在他旁邊的複選框必須更改ID(YES ID不類,絕對需要它的ID)使用jquery只選擇下一個

<table> 
<tr> 
    <td> 
    <input type="radio" class="attribute_radio" name="contenance" /> 
</td> 
<td><a href="">A super link</a> 
</td> 
<td>      
    <input type="number" name="qty" class="text" size="2" maxlength="3"/> 
</td> 
</tr> 
<tr> 
    <td> 
    <input type="radio" class="attribute_radio" name="contenance" /> 
</td> 
<td><a href="">A super 2nd link</a> 
</td> 
<td>      
    <input type="number" name="qty" class="text" size="2" maxlength="3"/> 
</td> 
</tr> 
</tr> 

但與我寫劇本,它的ID適用於所有數字中,而我只想按鈕旁邊的無線電

$(document).ready(function(){ 
$("input[type='radio']").click(function(){ 

    $("input[type='number']").attr('id', 'quantity_wanted'); 

}); 

});         

http://jsfiddle.net/micheler/y4jrotkc/4/

+2

_「絕對需要它是ID爲」 _ - 我對此表示懷疑。您似乎在那裏存儲數量值 - 爲此,_custom數據屬性_會比id更合適。 – CBroe

回答

4

首先ID的唯一一個必須是唯一的。我用sugest來代替class。我創建這個改變輸入的ID,如果無線電檢查:

$('input[type=radio]').click(function() { 
 
    //remove all id from input number 
 
    $('input[type=number]').removeAttr('id'); 
 
    //check if radio is checked and apply id to input number next to it 
 
    if($(this).prop("checked")) 
 
    $(this).next().attr('id', 'quantity_wanted') 
 
});
#quantity_wanted { 
 
    background-color: red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="page_navigation1">next</div> 
 
<button id="add">Add</button> 
 
<button id="remove">Remove</button> 
 
<p> 
 
    <input type="radio" class="attribute_radio" name="contenance" /> 
 
    <input type="number" id="" name="qty" class="text" size="2" maxlength="3" /> 
 
</p> 
 
<p> 
 
    <input type="radio" class="attribute_radio" name="contenance" /> 
 
    <input type="number" name="qty" class="text" size="2" maxlength="3" /> 
 
</p>

+2

很好的答案!你只是打敗了我。 – theonlygusti

+0

謝謝@theonlygusti :) –

+0

對不起,但我更新了我的代碼,因爲它不是我所需要的。你的腳本只更改了下一個元素的ID – Pourgroud