根据jQuery自动完成值设置单选按钮 [英] Setting a radio button based on jquery autocomplete value

查看:58
本文介绍了根据jQuery自动完成值设置单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表格,董事在向我的数据库添加事件时会填写表格.由于特定类型的事件可能会发生不止一次,因此我正在尝试使它们尽可能地容易.使用自动完成功能,我可以使文本值正常工作.单选按钮有问题.

I have a form that directors fill out when they add an event to my database. Since the particular type of event is likely to happen more than once, I am trying to make it as easy as possible for them. Using autocomplete, I get the text values to work just fine. Having problems with radio buttons.

示例:有3种可用格式-残障,刮擦或同时使用两种.差点是值1,临时值是2,两者都是值3.当他们第一次输入事件时,我可以将正确的值添加到表中.

Example: there are 3 types of formats available - handicap, scratch, or both. Handicap is value 1, scratch is value 2 and both is value 3. I am able to add the correct value to the table when they first enter their event. How do I set the radio button that corresponds (format_1 for handicap, format_2 for scratch, format_3 for both) using autocomplete?

 $('#format').val(ui.item.f); 

如果我使用文本而不是单选按钮,则会从表中返回值

will return the value from the table if I use text instead of radio button

我试图创建一个无效的变量(即var f = $(.format").val();).我在想的是与设置变量(例如,f)类似的东西,它会设置一个单选按钮(例如,format_f = checked),但还无法弄清楚如何对其进行机械化.无法从我的书或互联网上找到任何方向.

I tried to make a variable ( i.e. var f = $(".format").val();) which didn't work. What I was thinking about was something similar to setting a variable (f, for example), that would set a radio button (format_f = checked, for instance), but haven't been able to figure out how to mechanize it. Haven't been able to find any direction from my books or on the Internet.

有人可以指出正确的方向吗?

Can someone please point me in the right direction?

推荐答案

您需要将其注入到自动完成功能的 select 函数中.您的自动完成实例化可能没有一个,因为默认行为没有必要.

You need to inject this into the select function of your autocomplete. It's possible your instantiation of autocomplete does not have one, as it's not necessary for default behaviour.

显然,您需要具有自动完成选项和格式的映射,并且可以通过生成自动完成数据的任何方式将其发送回去.(在您的示例中尚不清楚自动完成功能是AJAX还是本地,但这无关紧要.)

Obviously you need to have the mapping of the autocomplete options and formats available, and you can have this sent back by whatever generates the autocomplete data. (It's not clear in your example whether your autocomplete is AJAX or local, but it doesn't matter.)

最终,您希望自动完成功能具有以下条目:

Ultimately you want your autocomplete to have entries like this:

[ { "id":"foo", "value":"bar", "f":"1" },
  { "id":"baz", "value":"quux", "f":"2" } ]

然后,自动完成操作结束时的选择功能将如下所示:

Then your select function at the end of your autocomplete will look like:

select: function(event, ui) {
    if (ui.item) {
        $('#format').val(ui.item.f);
    }
}

我在 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆