0

I know this is a silly question and I apologize for my limited knowledge in Javascript and programming in general.

I have a search that gets a value you enter and then uses it to search the db and returns the data in a table. So if I search banana it will display banana and it's values like fat, fiber etc. in a table, creating new rows for each search. Now, the problem I'm having is that if I search for let's say 3 items in the end I'll get 6 rows instead of 3, because the table will fill up with all the previous searches. This is the part of the code that handles this: It's part of the html file

     <script>

function funkcija(){
var value = $('a').text();

$('#hidden1').show();



$('#jedinice_butt').click(function(){
var odabrano = $("#dropdown option:selected").text();

var uneseno = $("#input_jedinica").val();
$('#tablica').show();




if(odabrano === "g"){

alert(value);
$.getJSON("nutritional_value.php?value=" + encodeURI(value), function (data) {
var ttr = $("<tr />");   
$.each(data, function(k, v){

    $("<td />").text(k=='name' ? v : v * (parseFloat(uneseno, 10) / 100)).appendTo(ttr);

});

$("#tejbl").append(ttr);
}); 



} 
}
</script>

part of the html:

 <div id="search_result">

    <a href="#"></a>

</div>

<div id="hidden1">


        <form name="input" action="" method="get">
             <input id="input_jedinica" type="text" name="namirnica">
             <select id="dropdown">
                <option value="kg">g</option>
                <option value="dg">dg</option>
                <option value="g">kg</option>
            </select>
            <button type="button" id="jedinice_butt">ok</button>
        </form>

</div>
</div>


<div id="tablica">
<table id="tejbl">

<tr id="naslov">
<td><h3>NAME</h3></td>
<td><h3>FAT</h3></td>
<td><h3>FIBER</h3></td>
<td><h3>SUGARS</h3></td>
</tr>


 </table>
 </div>

So how do I do it without having it output all of the previous searches every time I search for one item?

php for the second search, nutritional_value.php:

<?php



    include 'connect.php';


    $value = $_GET['value'];


   $query = mysql_query("SELECT NAME, FAT, FIBER, SUGARS FROM ccm WHERE NAME LIKE '$value%'");
   while( $run = mysql_fetch_array($query)){


        $results = array();
        $results["name"]=$run['NAME'];
        $results["fat"]=$run['FAT'];
        $results["fiber"]=$run['FIBER'];
        $results["sugars"]=$run['SUGARS'];
        //Send it to the client in json format:
        echo(json_encode($results));

  }




    ?>

php file for the first search:

<?php 

include 'connect.php';

$value = $_POST['value'];

echo '<ul>';

$query = mysql_query("SELECT NAME FROM ccm WHERE NAME LIKE '$value%'");
while( $run = mysql_fetch_array($query)){
    $name = $run['NAME'];


    echo '<li onClick="funkcija();">'.$name.'</li>';


}

echo '</ul>';
 ?>

jQuery for the first search:

$(document).ready(function(){

$('#search_button').click(function(){
    var value = $('#search_box').val();

    if(value != ''){
    $('#search_result').show();
        $.post('search.php', {value:value}, function(data){
            $('#search_result a').html(data);


        });
    }
    else{
        $('#search_result').hide();
    }
});


   });
4

1 回答 1

1

尝试将其更改为function funkcija():将其删除$('#jedinice_butt').click(function () {并粘贴到您$(document).ready(function () {的顶部就可以了。然后添加funkcija()});到它,所以你有$(document).ready(function () {funkcija()});. 你似乎);}从底部缺少了一个funkcija。复制和粘贴错误?如果它在您的实际代码中,您只需要删除最后一个});您的最终代码应该是:

$(document).ready(function () {

$('#jedinice_butt').click(function () {funkcija()});

$('#search_button').click(function () {
    var value = $('#search_box').val();

    if (value != '') {
        $('#search_result').show();
        $.post('search.php', {
            value: value
        }, function (data) {
            $('#search_result a').html(data);


        });
    } else {
        $('#search_result').hide();
    }
});


 });



function funkcija(){
var value = $('a').text();

$('#hidden1').show();

var odabrano = $("#dropdown option:selected").text();

var uneseno = $("#input_jedinica").val();
$('#tablica').show();




if(odabrano === "g"){

alert(value);
$.getJSON("nutritional_value.php?value=" + encodeURI(value), function (data) {
var ttr = $("<tr />");   
$.each(data, function(k, v){

$("<td />").text(k=='name' ? v : v * (parseFloat(uneseno, 10) / 100)).appendTo(ttr);

});

$("#tejbl").append(ttr);
}); 
}
}
于 2013-06-02T20:32:07.333 回答