gpt4 book ai didi

javascript - 通过自动完成设置隐藏单选按钮值

转载 作者:行者123 更新时间:2023-11-28 21:04:14 24 4
gpt4 key购买 nike

当用户向另一个用户发送消息时。他们可以选择发送到哪种类型的个人资料。 (通用或经理)...我正在后端检查要使用“recipient_type”将其发送到哪个配置文件,如何让我的自动完成功能为我选择隐藏的单选按钮?

自动完成看起来像这样:
:John Doe - 经理或者致: John Doe

模板:

<div class="hide">
<input type="radio" id="id_recipient_type" name="recipient_type" value="0" />
<input type="radio" id="id_recipient_type" name="recipient_type" value="1" />
</div>
<div class="inline-block">
<label for="id_omnibox"></label>
<input type="hidden" name="recipient_username" id="id_recipient_username" />
<input id="message-to" class="required input-text" style="width: 145%;"name="omnibox" placeholder="Search for user..." autocomplte="on" type="text" />
</div>

脚本:

$(document).ready(function(){
$.get('/autocomplete/message/', function(data) {
var completions = new Array();
var dict = JSON.parse(data, function(key, value) {
completions.push(key);
return value;
});
$('#message-to').autocomplete({
source: completions,
minLength: 1,
select: function(value, data){
$('#id_recipient_username').val(dict[data.item.value])
split_string = data.item.value.split("- ");
$('#id_recipient_type_'+(split_string[1]=="Manager"?"1":"0")).attr('checked', true);
}
});
});
});

最佳答案

似乎为了使您的代码正常工作,您需要更改或:

<div class="hide">
<input type="radio" id="id_recipient_type_0" name="recipient_type" value="0" />
<input type="radio" id="id_recipient_type_1" name="recipient_type" value="1" />
</div>

单选框 ID。或:

$('#id_recipient_type[value="'+(split_string[1]=="Manager"?"1":"0")+'"]').attr('checked', true);

#id_recipient_type[value="1"]#id_recipient_type[value="0"] 的 jquery 选择器。

我宁愿使用第一个解决方案,因为在 html 中 id 应该是唯一的。

您需要解决kmfk所述的问题在您的拆分中,当找不到 ' - ' 字符串时会抛出错误,因此请更改:

split_string = data.item.value.split("- ");

致:

split_string = 'John Doe - Manage'.match(/ - (Manager)$/)
split_string = split_string != null ? "0" : "1";

关于javascript - 通过自动完成设置隐藏单选按钮值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10250950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com