简体   繁体   English

使用 PHP 变量作为 ID 并从 Javascript 调用

[英]Using PHP Variable as ID and calling from Javascript

Im using this is an ID我使用的是一个 ID

 $id = $row['id'];
 echo "<input type='text' id='$id' value='$row["s_code"]'>";

How can I get that ID in javascript function?如何在 javascript 函数中获取该 ID? I want it in here...我要这里...

 var scode  = $('#').val();

While loop..虽然循环..

    while ($row = mysqli_fetch_array($sqlSimplex , MYSQLI_ASSOC)) {
        echo "<tr><td>";
        $id = $row['id'];
        echo "<input type='text' id='$id' value='".$row['s_code']."' readonly>";
        echo "</td><td>";
        echo $row['description'];
        echo "</td>";
        echo "<input type='hidden' id='comval'  value='$newProductID'>";
        echo "<td>";
        echo "<input type='text' size='7' id='userqtyval'>";
        echo "</td><td>";
        echo "<input type='button' onclick='addthis()'  value='Add!'>";
        echo "</td></tr>";  

    }

Javascript Javascript

  function addthis() {

            var myID = '$id';
            var scode = $("#"+myID).val();


            alert(scode);
                $.ajax({
                    type: 'POST',
                    url: 'insertsimplextoc_comp.php',
                    data: { simplexcode: scode },
                }); 
    }

Been stuck on this for over a week!坚持了一个多星期!

PHP: PHP:

while ($row = mysqli_fetch_array($sqlSimplex , MYSQLI_ASSOC)) {
    echo "<tr><td>";
    $id = $row['id'];
    echo "<input type='text' id='$id' value='".$row['s_code']."' readonly>";
    echo "</td><td>";
    echo $row['description'];
    echo "</td>";
    echo "<input type='hidden' id='comval'  value='$newProductID'>";
    echo "<td>";
    echo "<input type='text' size='7' id='userqtyval'>";
    echo "</td><td>";
    echo "<input type='button' class='submit-scode' data-field-id='$id' value='Add!'>";
    echo "</td></tr>";  
}

Javascript (using jQuery click event): Javascript(使用 jQuery click事件):

$('.submit-scode').click(function(){
    var field_id = $(this).attr('data-field-id');
    var scode = $("#"+field_id).val();

    $.ajax({
        type: 'POST',
        url: 'insertsimplextoc_comp.php',
        data: { simplexcode: scode },
    }); 
}); 

Untested, but it should work.未经测试,但它应该工作。

Add the id of the current loop as a parameter to the addthis function:将当前循环的 id 作为参数添加到 addthis 函数中:

echo "<input type='button' onclick='addthis(\"$id\")'  value='Add!'>";

Then grab the id when the function is called like so:然后在调用函数时获取 id,如下所示:

function addthis(myID)
{
     var scode = $("#"+myID).val();
     //etc
}

have to keep track of that dynamic id's必须跟踪该动态 ID
It you have that id then you can do something like this如果你有那个 id 那么你可以做这样的事情
var id="assign php varable"; var id="分配 php 变量";
var scode =$('#'+id).val(); var scode =$('#'+id).val();

I'm guessing you want it to be used in the addthis() function ?我猜你想在addthis()函数中使用它?

In this case, you'll hve to give a similar class to all your readonly inputs .在这种情况下,您必须为所有只读inputs提供一个类似的类。

Then, while clicking on the add button, you'll have to check for its parent <tr>然后,在单击添加按钮时,您必须检查其父<tr>

I made some example here : http://jsfiddle.net/captain_torche/frzadnbb/我在这里做了一些例子: http : //jsfiddle.net/captain_torche/frzadnbb/


Or, you could alteratively put the ID as a parameter of the addthis() function, like so :或者,您也可以将 ID 作为addthis()函数的参数,如下所示:

echo "<input type='button' onclick='addthis(".$id.")'  value='Add!'>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM