将其他表单元素传递给ajax建议框脚本(pass other form elements to ajax suggestion box script)

编程入门 行业动态 更新时间:2024-10-27 11:22:19
将其他表单元素传递给ajax建议框脚本(pass other form elements to ajax suggestion box script)

**移到这里: 许多类似的AJAX意见箱没有ID


我花了一些时间给我建立一个jQuery AJAX建议框脚本。 它很棒。 现在我正在努力学习它以修改它以做一些不同的事情。

现在,生成建议的脚本只接受查询文本,并且每次都进行相同的检查。 现在,我希望能够传递其他信息,以便在搜索框的每个实例中查找不同的内容。

HTML ......简单明了。

<input class="suggest" name="q" type="text" autocomplete="off" />

jQuery看起来像这样:

jQuery(document).ready(function() { $('.suggest').autocomplete( { source:'output.php', minLength:3, focus: function (event, ui) { $(event.target).val(ui.item.label); return false; } } ); [snip] }

代替:

source:'output.php', minLength:3,

我想要:

source:'output.php?arg1=blah1&arg2=blah2', minLength:3,

其中arg1和arg2在表单中传递...

<input class="suggest" name="q" type="text" autocomplete="off" /> <input type="hidden" name="arg1" value="blah1" /> <input type="hidden" name="arg2" value="blah2" />

这有意义吗?

多谢你们。 我知道关于OOL和javascript的“这么多”,所以我正在学习......

++++++++++++++++++++++++++

更新

这些不起作用......

// source:'/ suggest /?q ='+ \ $('input [name = q]')。val(),minLength:3,

// source:'/ suggest /?q ='+ \ $('input [type =“text”] [name =“q”]')。val(),minLength:3,

// source:'/ suggest /?q ='+ \ $('input [type =“text”]')。val(),minLength:3,

// source:'/ suggest /?q ='+ \ $('input:text')。val(),minLength:3,

// source:'/ suggest /?q ='+ \ $('input [type = text]')。val(),minLength:3,

......但这确实......

source:'/suggest/?q='+'alex', minLength:3,

有任何想法吗?

** moved to here: numerous similar AJAX suggestion boxes without ID's


I paid somebody to build me a jQuery AJAX suggestion box script a while back. It works great. Now I'm trying to learn about it enough to modify it to do something different.

Right now, the script that generates the suggestions just takes the query text and does the same check every time. Now, I want to be able to pass along additional info so different things are looked for with each instance of a search box.

The HTML... simple, obvious.

<input class="suggest" name="q" type="text" autocomplete="off" />

The jQuery looks like this:

jQuery(document).ready(function() { $('.suggest').autocomplete( { source:'output.php', minLength:3, focus: function (event, ui) { $(event.target).val(ui.item.label); return false; } } ); [snip] }

Instead of:

source:'output.php', minLength:3,

I'd like:

source:'output.php?arg1=blah1&arg2=blah2', minLength:3,

where arg1 and arg2 are passed along in the form...

<input class="suggest" name="q" type="text" autocomplete="off" /> <input type="hidden" name="arg1" value="blah1" /> <input type="hidden" name="arg2" value="blah2" />

Does this make sense?

Thanks guys. I know just about "this much" about OOL's and javascript, so I'm learning...

++++++++++++++++++++++++++

update

these don't work...

// source:'/suggest/?q='+\$('input[name=q]').val(), minLength:3,

// source:'/suggest/?q='+\$('input[type="text"][name="q"]').val(), minLength:3,

// source:'/suggest/?q='+\$('input[type="text"]').val(), minLength:3,

// source:'/suggest/?q='+\$('input:text').val(), minLength:3,

// source:'/suggest/?q='+\$('input[type=text]').val(), minLength:3,

... but this does...

source:'/suggest/?q='+'alex', minLength:3,

Any ideas?

最满意答案

您可以使用jQuery的.serialize()方法.serialize() 。 该文件表明它将:

将一组表单元素编码为字符串以进行提交。

这听起来像你想要做的。 你可以尝试这样的事情:

$('.suggest').autocomplete({ source:'output.php?' + $("#MY_FORM_ID").serialize(), minLength:3, focus: function (event, ui) { $(event.target).val(ui.item.label); return false; } });

编辑:请注意,使用字符串作为source不会导致URL随输入值动态更改。 为此,您需要使用回调函数并自行处理请求/响应。 我在上面显示的代码将在实例化自动完成窗口小部件时使用MY_FORM_ID的值。

You may be able to get away with using jQuery's .serialize() method. The documentation states that it will:

Encode a set of form elements as a string for submission.

Which sounds like what you're looking to do. You can try something like this:

$('.suggest').autocomplete({ source:'output.php?' + $("#MY_FORM_ID").serialize(), minLength:3, focus: function (event, ui) { $(event.target).val(ui.item.label); return false; } });

EDIT: Please note that using a string as the source will not cause the URL to change dynamically with the value of the input. To do that, you'll need to use a callback function and facilitate the request/response yourself. The code I show above will use the value of MY_FORM_ID at the time the autocomplete widget is instantiated.

更多推荐

minLength,source,AJAX,jQuery,电脑培训,计算机培训,IT培训"/> <meta name="

本文发布于:2023-07-05 01:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031225.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   脚本   元素   建议   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!