繁体   English   中英

字段启用禁用javascript不起作用

[英]field enable disable javascript not working

我有两个单选按钮,我想根据用户在单选按钮中选择的值在html页面上显示一些值。

我的html文件:

<form action="">
<input type="radio" name="calenders" value="calendar_details_b2b">B2B
<br>
<input type="radio" name="calenders" value="calendar_details_b2c">B2C
</form>
<div id="calendar_details_b2c" >
   content of B2C
</div>
<div id="calendar_details_b2b">
   content of B2B
</div>

这是运行正常的JS小提琴: https : //jsfiddle.net/0b0xp5xm/9/

现在,当我更改ID时,它不起作用, https://jsfiddle.net/vjLnou8h/1/

编辑

我已将此代码添加到我在new.hrml.erb页面上呈现的Rails应用程序_form.html.erb中。

<script    src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {

  $('input[type=radio][name=calenders]').change(function () {
  $('#calendar_details_b2b').css("display","none");
  $('#calendar_details_b2c').css("display","none");
  console.log($(this).val());
  var fieldToShow = $(this).val();
  $("#" + fieldToShow).css("display","block");
  });

});
</script>

但是它仍然无法正常工作。

添加JQuery引用后,它就可以正常工作。

https://jsfiddle.net/vjLnou8h/2/

 <html> <head> <style> #data_Analysis, #study_Design { display: none; } </style> </head> <body> <form action=""> <input type="radio" name="calenders" value="study_Design">B2B <br> <input type="radio" name="calenders" value="data_Analysis">B2C </form> <div id="study_Design">content of B2B</div> <div id="data_Analysis" >content of B2C</div> <!-- It's generally better to put your scripts just before the <body> tag closes because by the time the browser gets that far, the DOM has been read. Also, it can speed up page load times. --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> $('input[type=radio][name=calenders]').change(function () { $('#study_Design').css("display","none"); $('#data_Analysis').css("display","none"); console.log($(this).val()); var fieldToShow = $(this).val(); $("#" + fieldToShow).css("display","block"); }); </script> </body> </html> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM