簡體   English   中英

Laravel:onForm on :: text

[英]Laravel: onchange on Form::text

第一個Laravel項目。 每當更改Form :: text中的輸入時,我都想運行一個腳本。 到目前為止,我的代碼如下所示:

風景:

@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);"]) !!}

也如下所示開始表格

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

有關更多信息, 請訪問https://laravelcollective.com/docs/5.3/html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM