0

したがって、mySQL テーブルの .get() メソッドを使用する ajax 検索スクリプトがあります。

各行には、ボタン、2 つのテキスト フィールド、および ajax の結果から取得した値を持つ 1 つの非表示の入力があります。ボタンをクリックして特定の行の値を取得するにはどうすればよいですか?

私のajaxコード:

$("#cautare_p").keyup(function(){
    $.get("php/cautare/script_cautare.php","p="+$(this).val(),
        function(rez){
        $(".afisare_cautare").html(rez)
    })
    //end of search script
});

PHP コード:

echo "<table width='100%' border='1' cellpadding='1' cellspacing='1'>
    <td>Categorie</td>
    <td>Produs</td>
    <td>Cantitate</td>
    <td>Discount</td>
    <td></td>
    </tr>";

while(mysql fetch asocc code here){
$rr.="<tr><form>
         <td><span class='link'><a href='acasa.php?produse=lista&brand=$categId'>$numeCateg</a></span></td>
         <td><span class='link'><a href='acasa.php?produse=vizualizare&prodid=$prodId'>$numeProd</a></span>
         <br><span style='font-size:14px;'>U.M.:$um Stoc:$stoc Pret fara T.V.A.:$pretftva Pret cu T.V.A.:$pretctva</span>
         <input type='hidden' id='hiddenID' value='$prodId'>
         </td>
         <td><input type='text' id='text1' value='0'></td>
         <td><input type='text' id='text2' value='0'></td>
         <td>
         <input type='button' id='buton' value='Get'></td>
         </form>
         </tr>";
}
echo " $rr </table>";

結果の ajax コードは次のようになります。

$("#cautare_p").keyup(function(){
    $.get("php/cautare/script_cautare.php","p="+$(this).val(),
        function(rez){
        $(".afisare_cautare").html(rez),
    $("#buton").click(function(){
       $.get("php/cautare/insertInDb.php","h="+$("#hiddenID").val()+"&t1="+$("#tex1").val()+
       "&t2="+$("#hiddenID").val());
      });
    })
    //end of search script
});

解決済み:

$(".afisare_cautare table").find("tr").click(function(){
                var prodid=$(this).closest("tr").find("input:hidden:eq(0)").val();
                var cant=$(this).closest("tr").find("input:text:eq(0)").val();
                var discount=$(this).closest("tr").find("input:text:eq(1)").val();
                $("#test").text("ProdId: "+prodid+" Cantitate: "+cant+" Discount: "+discount);
            });
4

2 に答える 2

0

次のように、クリックしているのtr(スーパー親)にアクセスしようとすることができます。button

$(document).on("click", "input.get-data", function () {
   alert($(this).closest("tr").html());
});

デモ: http://jsfiddle.net/hungerpain/n7DQH/

クリックした場所からテキストボックスの値を取得したい場合は、

//to get the value of the text boxes from where you click,
$(document).on("click", "input.get-data", function () {
    //first text box
    alert($(this).closest("tr").find("input:text:eq(0)").val());
    //second text box
    alert($(this).closest("tr").find("input:text:eq(1)").val());
});

デモ: http://jsfiddle.net/hungerpain/n7DQH/1/

于 2013-06-15T13:25:18.780 に答える
0

外側の div を作成して、一意の ID を割り当てることができます。ajax コールバック メソッドで JavaScript を使用して、すべての内部データを動的に入力します。

このアプローチを使用すると、ページ上の任意のデータを参照できます。

于 2013-06-15T13:12:48.653 に答える