2014-06-29 27 views
-2

我正在嘗試構建JavaScript代碼。有兩個單選按鈕,其中一個有文本區域。JavaScript select radio單擊textarea

當點擊文本區域時,將選擇相同的單選按鈕。

Message: <input type="radio" style="float:left;" name="rdScriptChoice" value="now" /> 
<textarea name="txtVdoScript" id="word_count" cols="1" rows="1"> </textarea><br /> 
<input type="radio" name="rdScriptChoice" value="later" />I will message Later. 

這是小提琴。

http://jsfiddle.net/J2NHW/

+1

我看到這裏沒有問題。您需要詢問有關具體問題的明確和詳細的問題。如果你的代碼有問題,你需要直接在你的問題中提供一個功能完整而又簡單的問題示例,而不是鏈接到其他某個站點。 –

+2

'我正在嘗試構建JavaScript代碼'好的,繼續並開始吧!一旦你嘗試了一些東西,並遇到問題,請回頭! – admdrew

+1

那麼問題在哪裏? –

回答

0

你可以這樣做使用JQuery:

給第一個單選按鈕,例如ID:id="message"

$("#word_count").focus(function(){ 
    $("#message").prop("checked", true) 
}); 

DEMO

+0

非常感謝好友。你真的很棒。你邁出了正確的一步。所有其他的演講都沒有解決。 – user3788309

+0

爲什麼它不工作? – user3788309

+0

我的意思是在我的頁面上。我必須使用類似於$(document).ready(function() { – user3788309

0

如果你知道的基本知識的javaScript只是嘗試這種方式。使用它們的ID獲取dom元素,並在觸發textarea事件時操作它們。

HTML:

Message: <input type="radio" style="float:left;" id="msgRadio" name="rdScriptChoice"  value="now" /> 
<textarea name="txtVdoScript" id="word_count" cols="1" rows="1"> </textarea><br /> 
<input type="radio" name="rdScriptChoice" value="later" />I will message Later. 

JavaScript的:

var messageRadioButton = document.getElementById('msgRadio'); 

var textArea = document.getElementById('word_count'); 

textArea.onclick = function(){ 
    messageRadioButton.checked = true; 
}; 

jsFiddle