- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我设法将 Typeahead.js 与我的 laravel 应用程序集成。但不幸的是,我需要提交它的 ID
而不是它的 name
。我需要使用 Typeahead 检索类别的名称,但将其 ID 插入数据库,因为这就是我引用它的方式。
到目前为止,我收到了一个 sql 错误,因为它需要一个整数,但却得到了一个字符串(类别名称)。
我尝试执行 foreach
并将 $result->id
分配给 $data[]
但这根本不起作用。
我该如何解决?
PostsController
中的
getSubreddits
方法
public function getSubreddits($query) {
$results = Subreddit::select('name')->where('name', 'LIKE', '%' . $query . '%')->get();
return Response::json($results);
}
JS
<script type="text/javascript">
$(document).ready(function() {
var subreddits = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: 'http://localhost/reddit/public/data/subreddits',
remote: {
url: 'http://localhost/reddit/public/data/subreddits/%QUERY',
wildcard: '%QUERY'
}
});
$('#remote .typeahead').typeahead(null, {
name: 'name',
display: 'name',
source: subreddits
});
});
</script>
路线
Route::get('data/subreddits', 'PostsController@getSubreddits');
Route::get('data/subreddits/{QUERY}', 'PostsController@getSubreddits');
PostsController.php
中的整个store()
方法
public function store(PostRequest $request)
{
if (Input::has('link')) {
$input['link'] = Input::get('link');
$info = Embed::create($input['link']);
if ($info->image == null) {
$embed_data = ['text' => $info->description];
} else if ($info->description == null) {
$embed_data = ['text' => ''];
} else {
$extension = pathinfo($info->image, PATHINFO_EXTENSION);
$newName = public_path() . '/images/' . str_random(8) . ".{$extension}";
if (File::exists($newName)) {
$imageToken = substr(sha1(mt_rand()), 0, 5);
$newName = public_path() . '/images/' . str_random(8) . '-' . $imageToken . ".{$extension}";
}
$image = Image::make($info->image)->fit(70, 70)->save($newName);
$embed_data = ['text' => $info->description, 'image' => basename($newName)];
}
Auth::user()->posts()->create(array_merge($request->all(), $embed_data));
return redirect('/articles');
}
Auth::user()->posts()->create($request->all());
return redirect('/');
}
和形式
{!! Form::open(['url' => 'posts', 'method' => 'POST']) !!}
<p>
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', null, ['class' => 'form-control', 'id' => 'title']) !!}
</p>
<p>
{!! Form::label('link', 'Link:') !!}
{!! Form::text('link', null, ['class' => 'form-control', 'id' => 'link']) !!}
</p>
<p>
<div id="remote">
<input class="form-control typeahead" type="text" placeholder="Choose a Subreddit" name="subreddit_id">
</div>
</p>
<p>
{!! Form::submit('Submit Post', ['id' => 'submit', 'class' => 'btn btn-primary']) !!}
</p>
{!! Form::close() !!}
最佳答案
我成功了
<div id="remote">
<input class="form-control typeahead" type="text" placeholder="Choose a Subreddit" name="subreddit_name">
<input type="hidden" id="subreddit_id" name="subreddit_id" value="">
</div>
...
$('#remote .typeahead').bind('typeahead:select', function(ev, suggestion) {
$('#subreddit_id').val(suggestion.id);
});
关于php - Laravel 和 Typeahead.js : retrieve category name but submit its ID to the database?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32805185/
我创建了一个函数来在两个元素的 style.display 之间切换为“none”或“block”。我在这里遇到的问题是“发送”按钮似乎与“切换”按钮具有相同的行为。这不是我指定它要做的事情。我想知道
当我使用 validate.js 添加表单验证时,其验证正常并显示验证消息。但输入 type="submit"值在操作页面结构中显示两次。 为了说明,我有两个 .cfm 文件,index.cfm 具有
我有一个 s:form,里面有 sj:submit 和 s:submit,sj:submit 工作正常,但 s:submit 按钮不起作用。当我点击 s:submit 按钮时没有任何反应。 我尝试在表
参见http://jsfiddle.net/8KNc7/5/有关我正在讨论的内容的工作示例(目前在 Firefox 15 中进行测试,尚未尝试其他版本)。 我有一个带有 submit 输入和 butt
我有一个包含表单的 HTML 页面。我想做一些字段 "required" 。问题是我没有使用 在我的表单中,我使用 Javascript 函数来提交表单,因为我需要将 Javascript 变量发送到
如果用户提交表单,我想运行特定的 PHP block 。如果我使用带有 name="submit"的提交按钮并且: 我对 javascript 一无所知,我希望代码在用户更改下拉菜单时运行。如果我将
我有一个网站 www.abc.com,其中有一个页面 xyz.php。 xyz.php 采用表单提交事件读取数据并保存在数据库中。 www.abc.com 有一个针对 xyz.php 执行操作的表单。
这个问题在这里已经有了答案: JavaScript post request like a form submit (32 个答案) 关闭 9 年前。 我查看了不同的线程,但找不到合适的解决方案。
我有一个表单提交触发器和一个确认引导框,它显示在单击表单提交按钮上。在 bootbox 中确认如果用户同意我提交表单。我的问题是 onsubmit 在用户说是之前触发,并且当我在 bootbox 中调
我正在使用 JavaScript .submit() 函数提交我的表单。 form.submit(); 但是当我使用 addEventListener 来捕获我的提交事件时,它不起作用。 form.a
编辑:在考虑回答问题之前,请仔细阅读问题的所有内容。我既不是在生产代码中使用内联事件处理程序的可取性,也不是在实现我所引用的文章所 promise 的结果的最佳方法。这是关于Javascript语义和
这是我的形式,对我来说看起来不错。在这种形式中,我放置了这个按钮: 这是它调用的函数: function confirmSubmit() { // get the number of st
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
在表单上添加提交按钮的符合标准的正确语义方式是什么?我不需要按钮中的图像或背景,只需要简单的文本,也许是带弯 Angular 的渐变背景(使用 CSS3)。这些是我所知道的方法: click her
我的 App.js 包含以下代码: var app = angular.module('githubApp', []); 我有 githubAppController 和以下代码: app.contr
jQuery 允许通过以下任一方式以编程方式触发表单提交: $('.js-form-class-hook').submit(); $('.js-form-class-hook').trigger('s
我有一个表单,其中一个按钮使用 onClick 事件提交。 " 此外,我有一个输入,当输入处于焦点状态时按下回车键时会触发 couponButton。 虽然它们都触发相同的事件 (.submit()
目前只是尝试实现这些按钮:http://web.archive.org/web/20110721191046/http://particletree.com/features/rediscoverin
我正在提交我的第一个构建以供 TestFlight beta 审查,在创建一个组后,添加 4 名团队成员添加我的构建,取消单击该框以表示我需要登录信息来测试该应用程序,我单击“提交”审查。” iTun
我有一个带有 onsubmit 属性的表单。我需要绑定(bind)一个新的提交事件,并且我需要在任何现有提交函数之前执行此事件。 下面的代码演示了这个问题。 Test
我是一名优秀的程序员,十分优秀!