[英]Activate radiobutton when text box is selected
我正在嘗試在正在處理的表單上設置一些Javascript表單驗證。 我已經完成了所有的驗證工作,但是我要做的一件事似乎無法弄清楚,也找不到在線的許多文章。
我面臨的挑戰是,如果用戶想在文本框中輸入“其他”字段,我希望自動選中“其他”單選按鈕。 這是我在那里的代碼,顯然對我不起作用。
if (f.OtherTextValue.value !== '')
{
document.getElementById("OtherTextRadio").checked = true;
return false;
}
因此,我認為我想這樣做的是:如果OtherTextValue(我的文本框)不為空,則獲取我的單選按鈕的ID並將其選中/選中。
我在Javascript領域還算陌生,但是正在嘗試學習。
謝謝您的幫助。
要檢查使用prop
:
$('#OtherTextRadio').prop('checked', true);
我看不到您的代碼,因此最終應該看起來像這樣:
if($('#OtherTextValue').val() == "Other") {
$('#OtherTextRadio').prop('checked', true);
}
$(document).ready(function(){
if ($('#OtherTextValue').val('Other'))) {
$('#OtherTextRadio').prop('checked', true);
}
});
通常在單擊“其他”單選按鈕時,啟用textBox。但是您的想法似乎有所不同。 請做更多的研究。
如果您要在沒有JQuery的情況下執行此操作,如問題中的代碼所示,我在JSFiddle中模擬了一些可能會有所幫助的內容: http : //jsfiddle.net/xAcbm/
此處的關鍵是將您的支票鏈接到事件,在此示例中,我添加了一個簡單的按鈕:
<input type="radio" id="chlVal" name="selVal" value="Val 1"/>Value 1<br/>
<input type="radio" id="chkOther" name="selVal" value="Other"/>Other<br/>
<input type="text" id="txtOther">
<button id="btn" onclick="Javascript: checkOther()">Check Form</button>
Javascript是:
function checkOther()
{
if (document.getElementById('txtOther').value!='')
{
alert('Value in the box');
document.getElementById('chkOther').checked=1;
}
}
希望能有所幫助
使用onchange事件
<asp:radiobutton ID="OtherTextRadio" runat="server"></asp:radiobutton>
<asp:TextBox ID="TextBox1" runat="server" onchange="checkRadio();" ></asp:TextBox>
腳本如下
function checkRadio() {
if (document.getElementById("TextBox1").value != "") {
document.getElementById("OtherTextRadio").checked = true;
}
else document.getElementById("OtherTextRadio").checked = false;
}
$(document).ready(function(){
$('#sourcetext').on('click', function() {
$('#sourcecheck').prop('checked', true);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.