[英]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.