2017-09-27 169 views
0

我有2個單選按鈕。當單擊第一個單選按鈕文本框將打開,當點擊另一個單選按鈕時第二個文本框將打開..現在我想當我點擊在第二個單選按鈕上,第一個單選按鈕文本框應該清晰。如何通過單擊另一個單選按鈕清除第一個單選按鈕文本框的值

查看:

<label>Assign Credit Limit Amount/Days</label><br> 
<label for="chkYes">Credit Amount</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<input type="radio" id="chkYes" name="chkPassPort" onclick="ShowHideDiv()"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

<label for="chkNo">Day</label>&nbsp;&nbsp;&nbsp;&nbsp; 

<input type="radio" id="chkNo" name="chkPassPort" onclick="ShowHideDiv()" /> 
<div id="creditAmount" style="display: none"><br> 
    <label for="creditAmount">Credit Amount</label> 
    <input type="text" class="form-control" id="creditAmount" placeholder="creditAmount" name="creditAmount" maxlength="128"> 
</div> 
<div id="day" style="display: none"><br> 
    <label for="day">Day</label> 
    <input type="text" class="form-control" id="day" placeholder="day" name="day" maxlength="128"> 
</div> 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> 

的javascript:

<script type="text/javascript"> 
    function ShowHideDiv() { 
     var chkYes = document.getElementById("chkYes"); 
     var chkNo = document.getElementById("chkNo"); 
     var creditAmount = document.getElementById("creditAmount"); 
     creditAmount.style.display = chkYes.checked ? "block" : "none"; 
     var day = document.getElementById("day"); 
     day.style.display = chkNo.checked ? "block" : "none"; 
    } 
</script> 
+0

你能詳細闡述一下你的要求嗎? –

+0

最簡單的解決方案是: document.getElementById(id_of_textbox).value =「」; – Calaris

回答

0

如果條件添加和真實,復位輸入的值:

if (chkNo.checked) { 
     creditAmount.querySelector('input').value = ''; 
    } 

function ShowHideDiv() { 
 
    var chkYes = document.getElementById("chkYes"); 
 
    var chkNo = document.getElementById("chkNo"); 
 

 
    var creditAmount = document.getElementById("creditAmount"); 
 
    creditAmount.style.display = chkYes.checked ? "block" : "none"; 
 
    var day = document.getElementById("day"); 
 
    day.style.display = chkNo.checked ? "block" : "none"; 
 
    
 
    if (chkNo.checked) { 
 
     creditAmount.querySelector('input').value = ''; 
 
    } 
 
}
<label>Assign Credit Limit Amount/Days</label><br> 
 
           <label for="chkYes">Credit Amount</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 

 
           <input type="radio" id="chkYes" name="chkPassPort" onclick="ShowHideDiv()"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 

 
           <label for="chkNo">Day</label>&nbsp;&nbsp;&nbsp;&nbsp; 
 

 
           <input type="radio" id="chkNo" name="chkPassPort" onclick="ShowHideDiv()" /> 
 

 
            <div id="creditAmount" style="display: none"><br> 
 
            <label for="creditAmount">Credit Amount</label> 
 
            <input type="text" class="form-control" id="creditAmount" placeholder="creditAmount" name="creditAmount" maxlength="128"> 
 
            </div> 
 

 
            <div id="day" style="display: none"><br> 
 
            <label for="day">Day</label> 
 
            <input type="text" class="form-control" id="day" placeholder="day" name="day" maxlength="128"> 
 
            </div>

+0

是的!我知道了,它的工作原理!!! ...謝謝你。 –

+0

不客氣。 – JiangangXiong

0

要清除使用純JavaScript輸入字段的值可以使用:

document.getElementById("creditAmount").value = ""; 

這不會工作你的情況,因爲你在你的HTML有多個creditAmount標識:

<div id="creditAmount" style="display: none"><br> 

and

<input type="text" class="form-control amount-input" id="creditAmount" placeholder="creditAmount" name="creditAmount" maxlength="128"> 
0

您可以添加條件以跟蹤哪個按鈕被點擊以及要清除哪些輸入文本。

<script type="text/javascript"> 
function ShowHideDiv() { 
var chkYes = document.getElementById("chkYes"); 
var chkNo = document.getElementById("chkNo"); 
var creditAmount = document.getElementById("creditAmount"); 
creditAmount.style.display = chkYes.checked ? "block" : "none"; 
var day = document.getElementById("day"); 
day.style.display = chkNo.checked ? "block" : "none"; 

# condition goes here to clear input value 
chkYes.checked ? creditAmount.getElementsByTagName('input')[0].value = "" : day.getElementsByTagName('input')[0].value = "" 
</script> 
相關問題