簡體   English   中英

Javascript 輸入字段僅在顯示時才需要?

[英]Javascript input field only being required if shown?

我是 js/jquery 的新手,我希望您能通過對我的腳本進行簡單的編輯來幫助我。 我的腳本的基礎是我有一組單選按鈕,如果用戶選擇是 - 他們就完成了。 但是,如果他們單擊“否”,則會彈出一個新的輸入框並詢問更多信息。

不幸的是,我一直無法找到一種方法來使提交時需要彈出輸入框,因為當用戶單擊“是”時會出現問題,仍然需要,但被隱藏。

到目前為止,這是我的代碼:

function ShowHide(){
    if(document.getElementById('spellingN').checked){
        document.getElementById("spellingT").style.display = "block";
    } else {
        document.getElementById("spellingT").style.display = "none";
}
}

<div class="col-md-4">
<div class="radio"><b><input id="spellingY" name="spelling" onchange="ShowHide()" required="" type="radio" value="yes" /> Yes</b></div>

<div class="radio"><b><label for="radios-1"><input id="spellingN" name="spelling" onchange="ShowHide()" required="" type="radio" value="no" /> No </label></b></div>
</div>
</div>

<div id="spellingT" style="display: none;">
<div class="form-group"><b><label class="col-md-4 control-label" for="Spelling-Correction"><b>Question 2a</b> Type the grammatically correct version of the term.</label> </b>

<div class="col-md-4"><b><input class="form-control input-md" id="Grammar-Correction" name="Grammar-Correction" placeholder="" required="" style="width: 100%;" type="text" /></b></div>
</div>

我希望你能理解它並幫助我。 提前致謝!

您可以在您的函數中添加此檢查:

if(document.getElementById('spellingT').style.display == "block"){
    //your submission
}
// Element is hidden
if(document.getElementById('spellingT').offsetParent === null)
{

}
// Element is visible
else
{

}

有關更多詳細信息,請訪問https://stackoverflow.com/a/21696585/2240375

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM