gpt4 book ai didi

javascript - Laravel:在 Form::text 上 onchange

转载 作者:行者123 更新时间:2023-11-30 15:24:50 25 4
gpt4 key购买 nike

第一个 Laravel 项目。每当我更改 Form::text 中的输入时,我都想运行一个脚本。到目前为止,我的代码如下所示:

View :

@extends('layouts.master')
@section('title', 'Onchange test')
@section('main')
@parent
<script>
function showProduct(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var jsonObject = JSON.parse(this.responseText); document.getElementById("nameHint").innerHTML = jsonObject['name'];
}
};
xmlhttp.open("GET","script/getproduct.php?q="+str,true);
xmlhttp.send();
}
}
</script>
{{ Form::open(array('url' => '/test'))}}
<table>
<tr>
<td>{{Form::text('barcode', null,['onchange'=>"showProduct(this.value)"])}}</td>
<td>
<td><div id="nameHint">Test</div></td>
</tr>
</table>
@endsection

PHP

<?php
$q = intval($_GET['q']);

$sql = DB::select('select * from inventory where barcode = ? LIMIT 1', [$q]);
$name = $sql[0]->name;

$test = $name;
echo json_encode(array("name"=> $name));
?>

但它不刷新(它只显示默认的“测试”)我错了什么?

最佳答案

试试这个

{!! Form::text('barcode', null,['onchange'=>"showProduct(this.value);"]) !!}

Also start form as below

{!! Form::open(['url' => 'foo/bar']) !!}
//
{!! Form::close() !!}

For more follow https://laravelcollective.com/docs/5.3/html

关于javascript - Laravel:在 Form::text 上 onchange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43155280/

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