简体   繁体   English

如何根据访客联系记录预选单选按钮

[英]How to pre-select radio button based on visitors contact record

We have a signup form for existing customers and have all text input fields populating with their information (first name, last name and email address).我们为现有客户提供了一个注册表单,所有文本输入字段都填充了他们的信息(名字、姓氏和电子邮件地址)。 We also have a list of radio button group with three radio buttons and need to pre-populate (select) the radio button that corresponds with their contact record in our database.我们还有一个包含三个单选按钮的单选按钮组列表,需要预先填充(选择)与其数据库中的联系人记录对应的单选按钮。

I've mapped their contact record info to a var for this field.我已将他们的联系记录信息映射到此字段的 var。

var flavorFave = <span class=dbrecord >Favorite Ice Cream Flavor</span>

Here's the form code for the radio buttons:这是单选按钮的表单代码:

<input type="radio" class="custom-control-input" id="Vanilla" name="flavor" value="Vanilla">
<input type="radio" class="custom-control-input" id="Chocolate" name="flavor" value="Chocolate">
<input type="radio" class="custom-control-input" id="Strawberry" name="flavor" value="Strawberry">
...
...
...

How can we configure the form so that the correct radio button is checked if the variable "flavorFave" is defined?如果定义了变量“flavorFave”,我们如何配置表单以便检查正确的单选按钮? For example, if the variable "flavorFave" equals "Vanilla", then we want the "Vanilla" radio button to be checked.例如,如果变量“flavorFave”等于“Vanilla”,那么我们希望选中“Vanilla”单选按钮。

您可能会从数据库中获取数据并将具有相应值的数据设置为使用 jQuery 道具检查

.prop('checked', true);

The following grabs the 'flavor' preference from the database and stores it in a variable called 'flavorFave.下面从数据库中获取“风味”首选项并将其存储在名为“flavorFave”的变量中。 The following is proprietary to our system.以下内容是我们系统的专有内容。

var flavorFave = <span class=dbrecord >Favorite Ice Cream Flavor</span>

The source code would show the flavor instead of that span tag, such as this:源代码将显示风味而不是 span 标签,例如:

var flavorFave = "strawberry";

If the variable 'flavorFave' is not defined, it will default to 'Vanilla'.如果未定义变量“flavorFave”,则默认为“Vanilla”。 If the flavor is defined, it will pre-populated the corresponding radio button:如果定义了风味,它将预先填充相应的单选按钮:

if(flavorFave === ''){  
  $("[name=flavor][value='Vanilla']").prop("checked", true)
} else {
  $("[name=flavor][value=" + flavorFave + "]").prop("checked", true)
  };
});

我认为您需要检查它是否未定义?

if(typeof flavorFave !== "undefined"){ radiobtn = document.getElementById(flavorFave); radiobtn.checked = true; }

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

相关问题 如何通过基于javascript的json输入预先选择动态创建的单选按钮 - How to pre-select dynamically created radio button based on json input via javascript 使用jQuery在“名称”属性中带有“ []”括号时,如何预选单选按钮? - How to pre-select radio group button when “name” attribute has “[]” brackets in it using jQuery? 在Angular中使用双向绑定时,如何预选单选按钮? - How to pre-select radio button when using two-way binding in Angular? 如何使用 ngfor 循环索引值预选 [选中] 第一个单选按钮 - How to Pre-select [checked] first radio button using ngfor loop index value 如何基于url哈希值预先选择一个复选框并执行功能 - How to pre-select a checkbox, and perform function, based on url hash 默认情况下无法预先选择所需的单选框 - Not being able to pre-select desired radio box by default 如何在javascript的下拉列表中预先选择一个选项? - how to pre-select an option in a dropdown in javascript? 如何在 select2 multi select 中预选值? - How to pre-select values in select2 multi select? Angularjs:如何在页面加载时预先选择输入文本字段? - Angularjs: How to pre-select input text field on page load? 如何使用ng-checked预先选择angularJs中的复选框 - How to pre-select checkbox in angularJs with ng-checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM