我有php代码:

<?php
$produk = mysql_query("SELECT * FROM produk ORDER BY id_produk DESC");
while($p=mysql_fetch_array($produk)){
    <select name='urutan'  onChange='yesnoCheck(this);'>
        <option value='yes' selected>Yes</option>
        <option value='no'>No</option>
    </select>
    <div id='ifY' style='display: none;'>
        <input type=submit value=submit class=ui-btn-primary>
    </div>
    <div id='ifN' style='display: none;'>
        <input type=submit value=submit class=ui-btn-primary>
    </div>
}

在我的jquery代码中:
<script>
function yesnoCheck(that) {
    if (that.value == "other") {
        document.getElementById("ifNon").style.display = "block";
    } else {
        document.getElementById("ifNon").style.display = "none";
    }

    if (that.value == "yes") {
        document.getElementById("ifY").style.display = "block";
    } else {
        document.getElementById("ifY").style.display = "none";
    }

    if (that.value == "no") {
        document.getElementById("ifN").style.display = "block";
    } else {
        document.getElementById("ifN").style.display = "none";
    }

}
</script>

在循环数据中,我尝试运行选择选项来显示div,但它只在我的第一个数据中运行,所以不能在其他数据中运行。请帮帮我

最佳答案

试试这个,这个应该有用

<?php
$produk = mysql_query("SELECT * FROM produk ORDER BY id_produk DESC");
$i = 0;
while($p=mysql_fetch_array($produk)){ ?>
<select name='urutan'  onChange='yesnoCheck(this,<?= $i ?>);' rel="<?= $i ?>">
    <option value='yes' selected>Yes</option>
    <option value='no'>No</option>
</select>
<div id="ifY<?= $i ?>" style='display: none;'>
    <input type=submit value=submit class=ui-btn-primary>
</div>
<div id="ifN<?= $i ?>" style='display: none;'>
    <input type=submit value=submit class=ui-btn-primary>
</div>

<br>
<?php $i++; } ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
function yesnoCheck(that,i) {
if (that.value == "yes") {
    $("#ifY" + i).css("display","block")
    $("#ifN" + i).css("display","none")
} else if (that.value == "no"){
    $("#ifY" + i).css("display","none")
    $("#ifN" + i).css("display","block")
}

}

09-09 17:49