如果显示,只需要Javascript输入字段?

时间:2016-05-13 12:05:05

标签: javascript jquery html

我是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>

我希望你能够理解它并帮助我。提前谢谢!

2 个答案:

答案 0 :(得分:1)

您可以在函数中添加此检查:

if(document.getElementById('spellingT').style.display == "block"){
    //your submission
}

答案 1 :(得分:0)

// Element is hidden
if(document.getElementById('spellingT').offsetParent === null)
{

}
// Element is visible
else
{

}

有关详细信息,请访问https://stackoverflow.com/a/21696585/2240375

相关问题