2014-01-28 31 views
0

第一次嘗試,但我拉我的頭髮試圖讓基本功能工作。我在表單上有兩個單選按鈕,根據選擇哪一個按鈕,將以相同的形式更新輸入文本框的值。對於我的生活,我無法得到這個工作!如果我選擇更新「h2」標籤,或者甚至是輸入文本框旁邊的文本標籤的屬性,則相同的代碼將起作用。我的jQuery代碼如下:jQuery的單選按鈕...基本功能不能得到在使用jQuery工作

$(document).ready(function() { 
    $('input:radio[name=RGType]').click(function(){ 
     var CompType = ($('input:radio[name=RGType]:checked').val()); 
     //alert(CompType) // this works 
     $('#txt_Computername').text(CompType); 
    }); 
}); 

HTML的區域如下:

<tr> 
    <td style="text-align:right"><label for="RGType" id="t_Type">Computer Type:</label></td> 
    <td> 
     <label><input type="radio" name="RGType" value="SECDES" id="RGType_0">Desktop</label> 
     <label><input type="radio" name="RGType" value="SECLAP" id="RGType_1">Laptop</label> 
    </td> 
    </tr> 
    <tr> 
    <td style="text-align:right"><label for="txt_Computerame" id="t_Computername"><span class="required">* </span>Computer Name:</label></td> 
    <td><input id="txt_Computername" type="text" name="Computername" alt="Computer Name" style="width:60%" value="test" /></td> 
    </tr> 

用Firebug,它正確地識別在文本框中的當前值作爲「測試」 - 所以我必須找到正確的元素。如果我將要更新的文本更改爲#t_Computername(最新標籤),它會起作用!

我在想什麼???

感謝您的任何意見...對不起啞基本問題!

問候......

+0

忘了補充,用於直接上方的文本框中的單選按鈕的HTML是: – user1854380

+0

使用'.VAL()'代替'的.text()'。 – undefined

+1

你不會相信它,但[爲'jquery set input value'設置googleing](https://www.google.com/search?q=jquery+set+input+value)確實有幫助!作爲第一個結果,它顯示了[jQuery的'.val'方法](http://api.jquery.com/val/)的文檔。 –

回答

1

看起來像你想:

$('#txt_Computername').val(CompType); 

您需要使用val()input元素不text()

+0

完美!謝謝!告訴你我是個新手!? – user1854380

+0

@ user1854380很高興幫助!如果你覺得滿意,請接受答案:) – Felix

0

您需要更換

設定值
$('#txt_Computername').text(CompType); 

$('#txt_Computername').val(CompType); 
+0

這就是它!謝謝! – user1854380