javascript - Laravel: onchange on Form::text - Stack Overflow

First Laravel Project.I want to run a script whenever I change the input in my Form::text. So far my c

First Laravel Project. I want to run a script whenever I change the input in my Form::text. So far my code looks like this:

The 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

The 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));
?>

But it's not refreshing (it shows only the deffault "Test") What did I wrong?

First Laravel Project. I want to run a script whenever I change the input in my Form::text. So far my code looks like this:

The 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

The 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));
?>

But it's not refreshing (it shows only the deffault "Test") What did I wrong?

Share asked Apr 1, 2017 at 9:49 FeralheartFeralheart 1,9436 gold badges32 silver badges64 bronze badges 1
  • you cant apply onchange event to text input. it is for select tag.. use onblur. – f_i Commented Apr 1, 2017 at 10:10
Add a ment  | 

1 Answer 1

Reset to default 4

Try this

{!! 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./docs/5.3/html

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744924044a4601317.html

相关推荐

  • javascript - Laravel: onchange on Form::text - Stack Overflow

    First Laravel Project.I want to run a script whenever I change the input in my Form::text. So far my c

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信