提问者:小点点

javascript单选按钮表单


我是javascript的新手,我正在尝试编写一个简单的脚本,它将在选中单选按钮时打开1个表单,在单击第二个表单时打开另一个表单(当选择none时为none)。我肯定js代码是完全错误的,因为我是一个完全的js初学者,但是我使用了逻辑和一点谷歌来达到这个目的,我不知道我哪里出错了。

null

var ele1 = document.getElementsByClassName("form1");
var ele2 = document.getElementsByClassName("form2");

    if (document.getElementById('button1').checked)
    {
        ele1.style.display = "block";
    }

    if (document.getElementById('button2').checked)
    {
        ele2.style.display = "block";
    }
.form1 {
    display: none;
    background-color: red;
    width: 100px;
    height: 100px;
}

.form2 {
    display: none;
    background-color: blue;
    width: 100px;
    height: 100px;
}
<input type="radio" name="role" id="button1">
<input type="radio" name="role" id="button2">
<div class="form1">
</div>
<div class="form2">
</div>
<script src="/scripts/form.java"></script>

null


共2个答案

匿名用户

这段代码本身并没有错,但它只执行一次;当页面加载时。相反,您希望在更改输入时切换表单。

为此,可见性代码被包装在一个函数中。然后,该函数被注册为输入元素上的事件侦听器,以便它在/codes更改时执行。

我还做了一些其他的改动:

  • 只使用 /code>元素以获得更好的语义。所有表单都必须包装在/code>的某个位置。/li> 更改为。不要把JavaScript和Java混在一起。/li> /codes上的以更好地描述它们的角色。/li>
<input type="radio" name="formID" id="input1">
<input type="radio" name="formID" id="input2">

<form id="form1">
  <!-- fields -->
</form>

<form id="form2">
  <!-- fields -->
</form>

<script src="/scripts/form.js"></script>
// form.js

// Get references to important elements.
var elInput1 = document.getElementById('input1');
var elInput2 = document.getElementById('input2');
var elForm1 = document.getElementById('form1');
var elForm2 = document.getElementById('form2');

// Define an event handler function.
function updateFormVisibility(event) {
  var elSelectedInput = event.target;

  if (elSelectedInput.id === 'input1') {
    elForm1.style.display = 'block';
    elForm2.style.display = '';
  } else {
    elForm1.style.display = '';
    elForm2.style.display = 'block';
  }
}

// Register the function as a handler for any `'input'` events that occur on the
// two radio button elements.
elInput1.addEventListener('input', updateFormVisibility);
elInput2.addEventListener('input', updateFormVisibility);

匿名用户

根据@Mehdi Brillaudt的回答:https://stackoverflow.com/a/42488571/13695248,您可以用jQuery尝试一下:

null

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label><input type="radio" class="form-switch" name="colorCheckbox" value="red" data-id="a" checked> red</label>
<label><input type="radio" class="form-switch" name="colorCheckbox" value="green" data-id="b"> green</label>
<label><input type="radio" class="form-switch" name="colorCheckbox" value="blue" data-id="c"> blue</label>

<div class="form form-a active"> form a </div>
<div class="form form-b"> form b </div>
<div class="form form-c"> form c</div>
.form {
  display: none;
}
.form.active {
  display: block
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
  $('.form-switch').on('change', function() {
    $('.form').removeClass('active');
    var formToShow = '.form-' + $(this).data('id');
    $(formToShow).addClass('active');
  });
});
</script>