weixin_33725272 2014-03-22 17:51 采纳率: 0%
浏览 14

按Enter时的ajax表单

//UPDATE

$('#codice_prodotto').on('keyup', function(e) {
     if (e.which === 13) {
         e.preventDefault();
         $('#cerca_prodotto').trigger('click');
     }
});

this work in real time, but if I press <kbd>Enter</kbd> the script redirect me in domain/?codice_prodotto=val

Any solutions?

//END UPDATE

I have problems with ajax request, this is the index page:

<script>
 $(document).ready(function() {
$("#cerca_prodotto").click(function(){
    var dati = $("#form-cerca-prod").serialize();
    $.ajax({
        type: "POST",
        url: "product/show_prod.php",
        data: dati,
        dataType: "html",
        success: function(msg){ $("#risultato").html(msg); },
        error: function(){ alert("Ricerca fallita, riprovare..."); }
    });
});
});
</script>
<form id="form-cerca-prod">
   <input name="codice_prodotto" type="text" placeholder="Codice Prodotto" id="codice_prodotto">
   <br><br>
   <input type="button" id="cerca_prodotto" value="Cerca">
</form>

<div id="risultato">
</div>

And this product/show_prod.php

    <?php
include "../config/config.inc.php";
$cod=urldecode($_POST['codice_prodotto']);
$q=mysql_query("SELECT * FROM prodotto WHERE id='$cod'");
if(mysql_num_rows($q)<1)
    echo "Nessun prodotto trovato!";
else{
    $p_id=mysql_result($q,0,"id");
    $p_descr=mysql_result($q,0,"descrizione");
    $p_prezzo=mysql_result($q,0,"prezzo");
    $p_prezzo=number_format($p_prezzo,2,',','.');
    ?>
    <br><br>
    <div style="border: 1px solid #e9e9e9; padding: 6px; border-radius: 5px;">
        <table border="0" align="center" style="color: #fff;">
            <tr>
                <td align="right">
                    Codice :
                </td>
                <td align="left">
                    <b><? echo $p_id; ?></b>
                </td>
            </tr>
            <tr>
                <td align="right">
                    Nome :
                </td>
                <td align="left">
                    <code><? echo $p_descr; ?></b>
                </td>
            </tr>
            <tr>
                <td align="right">
                    Prezzo :
                </td>
                <td align="left">
                    <b>&euro; <? echo $p_prezzo; ?></b>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2" style="padding: 7px;">
                    Quantit&agrave;: <input type="number" min="6" value="6" name="quantita" style="width: 40px;"><br><button type="submit" id="agg_prodotto"><img src="img/cart.png"> Aggiungi al carrello</button>
                </td>
            </tr>
        </table>
    </div>
    <?
}
?>

if I click on the button, the script works! if I enter a code and press <kbd>Enter</kbd> for the keyboard the script not work!

How I can solve this?

  • 写回答

4条回答

      报告相同问题?

      悬赏问题

      • ¥15 GEO下载数据的处理报错 :函数‘Meta’标签‘"data.frame"’找不到继承方法,如何解决?
      • ¥15 DLNM模型是否可以用二分类变量作为y变量
      • ¥15 android object box 一个实体多个表怎么写
      • ¥15 temux 启用docker 服务失败
      • ¥15 Flask 使用celery发送邮件出现‘目标计算机积极拒绝‘
      • ¥60 老人用的sd卡在手机里面不知道操作了什么,导致图片和视频变成了文件,取下sd卡连接电脑就是图中的样子,后缀改为.jpg才可以,需要用系统的画图软件才能打开,文件属性还是文件,有没有批量操作的解决办法
      • ¥15 超时跳出方法代码的返回值问题
      • ¥15 汇编语言程序设计设计,ascii码求数,再求数的BCD码
      • ¥30 Mask rcnn训练自己的数据集出现问题!
      • ¥20 研究人工智能时的几个问题