JavaScript - GetElementByid وساعدني
-
21-09-2019 - |
سؤال
<script type="text/javascript">
function myfunction() {
if (document.getElementById('myform').value == "yes") {
document.getElementById('yesinput').style.display = '';
} else {
document.getElementById('yesinput').style.display = 'none';
}
}
</script>
<select name="myform" id="myform" onchange="myfunction()">
<option selected="selected">please select</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<div id="yesinput" style="display:none">
<input name="input" type="text" />
</div>
<div id="noinput" style="display:none">
<input name="input" type="text" />
</div>
ساعدك. كيف نجعل إذا اخترنا رقم سيكون لديه حقل إدخال آخر أسفل المعرف = "noinput".
الآن يعمل إذا اخترنا نعم. اسمحوا لي أن أعرف
المحلول
function myfunction() {
if (document.getElementById('myform').selectedIndex == 0) {
document.getElementById('noinput').style.display = 'none';
document.getElementById('yesinput').style.display = 'inline';
} else {
document.getElementById('noinput').style.display = 'inline';
document.getElementById('yesinput').style.display = 'none';
}
}
نصائح أخرى
يمكنك إنشاء عنصر جديد وإلحاقه بـ DOM عبر JavaScript. مثال:
var newInput = document.createElement("input");
newInput.setAttribute("id", "newElement");
newInput.setAttribute("type", "text");
document.body.appendChild(newInput);
لا تنتمي إلى StackOverflow