javascript - 根据下拉选择添加 Bootstrap 字段

标签 javascript jquery html twitter-bootstrap bootstrap-4

我有一个下拉列表,其中我根据数字选择列出了用户数量。因此,假设他们从列表中选择了“7”位用户,它将显示两个额外的文本框等等。

代码如下:

	<script>
	jQuery(document).ready(function($){
	  $('select[name=totalUsers]').change(function () {
		$('.six').hide();
		$('.seven').hide();
		$('.eight').hide();
		$('.nine').hide();
		$('.ten').hide();
			
		$("select[name=totalUsers] option:selected").each(function () {
			var value = $(this).val();
			if(value == "six") {
				$('.six').show();
			
			} else if(value == "seven") {
				$('.seven').show();
			
			} else if(value == "eight") {
				$('.eight').show();
			
			} else if(value == "nine") {
				$('.nine').show();

			} else if(value == "tenUsers") {
				$('.ten').show();
			}
		});
	  });
  });
	</script>
<div class="container">
<form id="myform" data-toggle="validator">
  <fieldset>

	<div class="form-group row">
  		<label for="totalUsers" class="col-lg-10 col-form-label">Number of Attendees:</label>
  		<select class="form-control" id="totalUsers">
			<option value="five" selected>5</option>
			<option value="six">6</option>
			<option value="seven">7</option>
			<option value="eight">8</option>
			<option value="nine">9</option>
			<option value="ten">10</option>
  		</select><div class="help-block with-errors"></div>
	</div>
	
	<div class="form-group row one">
	  <label for="oneUser" class="col-lg-10 col-form-label">Names of Attendees:</label><input class="form-control" name="oneUser" type="text" id="oneUser" data-minlength="2" data-error="Please enter a valid name." placeholder="Ex: John Smith" required/><div class="help-block with-errors"></div>
	</div>
	<div class="form-group row two">
	  <input class="form-control" name="twoUsers" type="text" id="twoUsers" data-minlength="2" data-error="Please enter a valid name."required/><div class="help-block with-errors"></div>
	</div>
	<div class="form-group row three">
	  <input class="form-control" name="threeUsers" type="text" id="threeUsers" data-minlength="2" data-error="Please enter a valid name." required/><div class="help-block with-errors"></div>
	</div>
	<div class="form-group row four">
	  <input class="form-control" name="fourUsers" type="text" id="fourUsers" data-minlength="2" data-error="Please enter a valid name." required/><div class="help-block with-errors"></div>
	</div>
	<div class="form-group row five">
	  <input class="form-control" name="fiveUsers" type="text" id="fiveUsers" data-minlength="2" data-error="Please enter a valid name." required/><div class="help-block with-errors"></div>
	</div>
	<div class="form-group row six">
	  <input class="form-control" name="sixUsers" type="text" id="sixUsers" data-minlength="2" data-error="Please enter a valid name." required/><div class="help-block with-errors"></div>
	</div>
	<div class="form-group row seven">
	  <input class="form-control" name="sevenUsers" type="text" id="sevenUsers" data-minlength="2" data-error="Please enter a valid name." required/><div class="help-block with-errors"></div>
	</div>
	<div class="form-group row eight">
	  <input class="form-control" name="eightUsers" type="text" id="eightUsers" data-minlength="2" data-error="Please enter a valid name." required/><div class="help-block with-errors"></div>
	</div>
	<div class="form-group row nine">
	  <input class="form-control" name="nineUsers" type="text" id="nineUsers" data-minlength="2" data-error="Please enter a valid name." required/><div class="help-block with-errors"></div>
	</div>
	<div class="form-group row ten">
	  <input class="form-control" name="tenUsers" type="text" id="tenUsers" data-minlength="2" data-error="Please enter a valid name." required/><div class="help-block with-errors"></div>
	</div>
		
	<div class="form-group row col-lg-8">
		<button type="submit" name="singlebutton" class="btn btn-success" id="submit">Submit</button>
		<button type="reset" name="cancelbutton" class="btn btn-warning" id="cancel" onclick="window.location.href=''">Cancel</button>
	</div>

我将我的 jQuery 设置为它应该根据数字选择显示/隐藏文本字段的位置,但它对我不起作用 - 所有帮助将不胜感激。

最佳答案

Jerdine Sabio 的答案有效,但如果您想以更优雅的方式编写它,那么您可以在 switch 语句中这样做:
而不是:

// Use switch for cleaner code
    switch (selectValue) {
      case "five":
        $('.five').show();
      case "six":
        $('.five').show();
        $('.six').show();
        break;
      case "seven":
        $('.five').show();
        $('.six').show();
        $('.seven').show();
        break;
      case "eight":
        $('.five').show();
        $('.six').show();
        $('.seven').show();
        $('.eight').show();
        break;
      case "nine":
        $('.five').show();
        $('.six').show();
        $('.seven').show();
        $('.eight').show();
        $('.nine').show();
        break;
      case "ten":
        $('.five').show();
        $('.six').show();
        $('.seven').show();
        $('.eight').show();
        $('.nine').show();
        $('.ten').show();
        break;
    }

省略中断并使用 fall-through:

switch (value) {
    case "ten":
        $('.ten').show();
    case "nine":
        $('.nine').show();
    case "eight":
        $('.eight').show()
    case "seven":
        $('.seven').show();
    case "six":
        $('.six').show();
    case "five":
        $('.five').show();
}

这意味着如果您选择 ten,则 switch 语句将执行 ten 情况下的所有代码。

关于javascript - 根据下拉选择添加 Bootstrap 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52371673/

相关文章:

javascript - 在 Angular 2 Google map 上移动 map 控件

javascript - jQuery 滑动表头的问题

javascript - 避免 URL 编码

javascript - 如何制作一个文本不可突出显示的网页?

javascript - 查找动态生成表的表行索引

javascript - 对更改不同事件时的两个值求和

javascript - jquery ajax get 没有数据时显示错误信息

javascript - 将三个数组合二为一

javascript - 我登录 Google 帐户后没有任何反应

javascript - jQuery 似乎无法识别 Ajax 调用状态代码 403 的不同字符串