我在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的选项,但它只是在我的第一个数据中运行,因此无法在另一个数据中运行。请帮帮我
答案 0 :(得分:1)
这样的事情对我有帮助。 id也应始终是唯一的使用类。
PHP代码和HTML
<?php
$produk = mysql_query("SELECT * FROM produk ORDER BY id_produk DESC");
while($p=mysql_fetch_array($produk)){ ?>
<div> // new div added here.
<select name='urutan' onChange='yesnoCheck(this);'>
<option value='yes' selected data-selector="ifY">Yes</option> // added new data-selector attribute.
<option value='no' data-selector="ifN">No</option>
</select>
<div class='ifY commonclass' style='display: none;'> // changed id to class and added a commonclass to get all divs.
<input type=submit value=submit class=ui-btn-primary>
</div>
<div class='ifN commonclass' style='display: none;'>
<input type=submit value=submit class=ui-btn-primary>
</div>
</div>
<?php } ?>
Jquery的
$('[name="urutan"]').change(function(){
var $this = $(this);
var $parentDiv = $this.parent();
var $selected = $("option:selected",$this),
$parentDiv.find('.commonclass').hide();
$parentDiv.parent().find("."+$selected.data('selector')).show();
}).trigger('change');
答案 1 :(得分:1)
试试这个,这应该有效
<?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")
}
}
答案 2 :(得分:1)
使用动态ID和div的名称,并使用计数器变量选择元素。
PHP代码
<?php
$produk = mysql_query("SELECT * FROM manufacturer ORDER BY id DESC limit 5");
$i = 1;
while($p=mysql_fetch_array($produk))
{
?>
<select name='urutan<?php echo $i?>' id="urutan<?php echo $i?>" onChange='yesnoCheck("<?php echo $i?>");'>
<option value='yes' selected>Yes</option>
<option value='no'>No</option>
</select>
<div id='ifY<?php echo $i?>' style='display: none;'>
<input type=submit value=submit class=ui-btn-primary>
</div>
<div id='ifN<?php echo $i?>' style='display: none;'>
<input type=submit value=submit class=ui-btn-primary>
</div>
<?php
$i++;
}
?>
Javascript代码
<script>
function yesnoCheck(id)
{
var that = document.getElementById("urutan"+id);
/*if (that.value == "other") {
document.getElementById("ifNon"+id).style.display = "block";
} else {
document.getElementById("ifNon"+id).style.display = "none";
}*/
if (that.value == "yes") {
document.getElementById("ifY"+id).style.display = "block";
} else {
document.getElementById("ifY"+id).style.display = "none";
}
if (that.value == "no") {
document.getElementById("ifN"+id).style.display = "block";
} else {
document.getElementById("ifN"+id).style.display = "none";
}
}
</script>