如何从 JQuery UI 自动完成搜索结果中填充选择元素?

发布于 2024-11-08 00:16:34 字数 1346 浏览 0 评论 0原文

我对 JQuery 和 Javascript 相当陌生...

目前我正在使用以下技术或代码从 JQuery UI 自动完成搜索结果集填充输入框...

    var PopulateFields = function(event, ui){
                $('#Custid').val(ui.item.Custid);
                $('#Alpha1').val(ui.item.Alpha1);
                $('#Alpha2').val(ui.item.Alpha2);
                $('#CustName').val(ui.item.CustName);
                $('#Address1').val(ui.item.Address1);
                $('#Address2').val(ui.item.Address2);
                $('#City').val(ui.item.City);
                $('#State').val(ui.item.State);
    }
    $( "#search-by-custname" ).autocomplete({
                source: "cust_search_by_name.php",
                minLength: 4,
                select: PopulateFields
    });

php 脚本从数据库中获取信息并将其喷回通过以下格式的 JSON:

    [{"label":"A Tire Store","value":"A Tire Store","Custid":"10000","Alpha1":"COD123456","Alpha2":"TIRE","CustName":"A Tire Store","Address1":"123 Cherry Lane","Address2":"","City":"City of Bla","State":"FL","Zip":"555555"}]

这与输入框配合得非常好。
但是,如果我想让“状态”成为一个选择框,我将如何使用客户记录中的现有值填充该选择框?

或者说,这是不可能的吗?

我正在尝试创建一个数据输入表单,我想要的功能之一是当他们搜索要编辑的客户记录时,他们的搜索将填充表单上的所有字段,然后他们可以编辑它们。

我认为状态的选择框会为我节省一些数据验证逻辑...

我的另一个想法是使用状态的只读输入框(因为我知道 JQuery UI 自动完成将填充它),然后创建一个“状态选择器” " 选择将填充只读输入“状态”框以重新提交的框。

想法?

I am fairly new to JQuery and Javascript...

Currently I am using the following technique or code to populate input boxes from a JQuery UI autocomplete set of search results...

    var PopulateFields = function(event, ui){
                $('#Custid').val(ui.item.Custid);
                $('#Alpha1').val(ui.item.Alpha1);
                $('#Alpha2').val(ui.item.Alpha2);
                $('#CustName').val(ui.item.CustName);
                $('#Address1').val(ui.item.Address1);
                $('#Address2').val(ui.item.Address2);
                $('#City').val(ui.item.City);
                $('#State').val(ui.item.State);
    }
    $( "#search-by-custname" ).autocomplete({
                source: "cust_search_by_name.php",
                minLength: 4,
                select: PopulateFields
    });

The php script is grabbing the info from a database and squirting it back via JSON in the following format:

    [{"label":"A Tire Store","value":"A Tire Store","Custid":"10000","Alpha1":"COD123456","Alpha2":"TIRE","CustName":"A Tire Store","Address1":"123 Cherry Lane","Address2":"","City":"City of Bla","State":"FL","Zip":"555555"}]

This works wonderfully with input boxes.
However, if I wanted to make State a select box, how would I populate that Select box with the existing value in the customer record?

Or, is that impossible?

I am trying to create a data entry form, and one of the features I want to have is when they search for a customer record to edit, that their search will populate all fields on the form and then they can edit them.

I thought a select box for State would save me some data validation logic...

My other thought was to use a read only input box for the State (because I know JQuery UI autocomplete will populate that), and then create a "State Selector" Select box that would populate the read only input "State" box for re-submission.

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃梦幻 2024-11-15 00:16:35

你是说你已经尝试过但失败了?因为如果您有一个类似的选择,

<select id="State">
    <option>AK</option>
    <option>FL</option>
    <option>WY</option>
</select>

则调用 $("#State").val("FL") 将会起作用。

Are you saying you've tried this and it's failed? Because if you have a select similar to

<select id="State">
    <option>AK</option>
    <option>FL</option>
    <option>WY</option>
</select>

then calling $("#State").val("FL") will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文