简体   繁体   English

在表单上选择单选按钮时的 Javascript VISIBLE 和 REQUIRED 字段

[英]Javascript VISIBLE and REQUIRED field upon radio button choice on form

I am trying to write a function that will show a field based on the choice of a radio button, AND make that field required.我正在尝试编写一个函数,该函数将根据单选按钮的选择显示一个字段,并使该字段成为必需。 BUT, if the the said choice is not made, then the field is hidden and not required.但是,如果未进行上述选择,则该字段将被隐藏且不需要。 I have the toggle view working - meaning it shows if the first radio button is chosen, but I have not been able to get it to be required.我有切换视图工作 - 这意味着它显示是否选择了第一个单选按钮,但我无法获得它。 The field SSN is only visible if the choice 1 of field SocialSecurity is chosen.字段 SSN 仅在选择字段 SocialSecurity 的选项 1 时可见。

Here is the radio button on the form:这是表单上的单选按钮:

Do you have a Social Security Number?你有社会安全号码吗?

  • (choice 1) As a US Citizen, permanent resident, or temporary working resident, I have a Social Security Number. (选择 1)作为美国公民、永久居民或临时工作居民,我有一个社会安全号码。

  • (choice 2)Due to my international student status, my residency status, or my specific visa type, I do not have a Social Security Number. (选择 2)由于我的国际学生身份、我的居留身份或我的特定签证类型,我没有社会安全号码。

Please look at my code:请看我的代码:

$('.field.SocialSecurity input[type=radio]').on("change", function()
{
  switch($(this).val())
  {
    case 'As a U.S. Citizen, permanent resident, or temporary working resident, I have a Social Security Number.':
                $('.field.SSN').show();
                $('.field.SSN').attr('required', '');
                $('.field.SSN').attr('data-error', 'Please enter your Social Security Number.');
      break;

    case 'Due to my international student status, my residency status, or my specific visa type, I do not have a Social Security Number.':
        $('.field.SSN').hide();
        $('.field.SSN').removeAttr('required');
        $('.field.SSN').removeAttr('data-error');

      break;
  }     
});

Sometimes it depends on your jquery version why the attr function does not work.有时这取决于您的jquery版本为什么attr函数不起作用。 Instead of calling the removeAttr method, can you also set the value to false explicitly?除了调用 removeAttr 方法,您还可以将值显式设置为 false 吗? Try this out:试试这个:

.attr(attribute,value); // syntax

.attr("required", true);
// is the equivalent for required="required"

.attr("required", false);

// Using .prop // 使用 .prop

.prop(property,value) // syntax

.prop("required", true);
// is the equivalent for required=""

.prop("required", false);

Like this?像这样?

 $('.field.SocialSecurity input[type=radio]').on("change", function()
{
  switch($(this).val())
  {
    case 'As a U.S. Citizen, permanent resident, or temporary working resident, I have a Social Security Number.':
                $('.field.SSN').show();
                $('.field.SSN').prop('required', true);
                $('.field.SSN').prop('data-error', 'Please enter your Social Security Number.');
      break;

    case 'Due to my international student status, my residency status, or my specific visa type, I do not have a Social Security Number.':
        $('.field.SSN').hide();
        $('.field.SSN').prop('required', false);

      break;
  }     
});

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

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