使用Jquery/Javascript查找多个PHP生成的输入
我似乎无法理解这一点。。 我正在尝试使用Javascript选择一个输入值。问题是表单通过一个php数组循环,该数组获取eve,使每个表单都具有相同的名称,并且输入的名称相同。javascript只允许我获取第一个表单的值。我曾尝试使用使用Jquery/Javascript查找多个PHP生成的输入,php,javascript,jquery,ajax,xhtml,Php,Javascript,Jquery,Ajax,Xhtml,我似乎无法理解这一点。。 我正在尝试使用Javascript选择一个输入值。问题是表单通过一个php数组循环,该数组获取eve,使每个表单都具有相同的名称,并且输入的名称相同。javascript只允许我获取第一个表单的值。我曾尝试使用[]将表单放入数组,但我无法得到它。。请帮忙 $find = mysql_query(" SELECT `name`, `desc`, LEFT(`desc`, 80),`price`, `page`, `id` FROM `b
[]
将表单放入数组,但我无法得到它。。请帮忙
$find = mysql_query("
SELECT `name`, `desc`, LEFT(`desc`, 80),`price`, `page`, `id`
FROM `bang`
ORDER BY id ASC LIMIT 2 ") or die(mysql_error());
while($resultt= mysql_fetch_array($find)){
echo '<form id="linkcenter" action="">';
echo '<input type="hidden" value="'.$resultt['id'].'" id="linkcenterr" name="linkcenterr"';
echo '<input type="button" name="butn" value="Send" onClick="loadInfoo()">';
echo '</select>';
echo '</form>';
}
我使用jquery的目的就是制作一个框的动画,然后这个输入通过javascript发送到一个php文件,该文件运行一个sqlquery并回显数据库中的所有数据
我知道所有其他代码都在工作问题是我不知道如何将这些表单格式化为数组,以便javascript可以在单击时获取每个表单的值。HTML元素中的
id
属性必须是文档的唯一属性。您对所有表单输入和标记使用相同的id='linkcenter'
,这是无效的。大多数浏览器只会选择第一个
必须为每个节点指定唯一的ID:
while($resultt= mysql_fetch_array($find)){
// Use $resultt['id'] in the node id
echo '<form id="linkcenter' . $resultt['id'] . '" action="" class="linkcenter-form">';
// Use $resultt['id'] with another string in the node id
// Don't forget to close the tag > here.
echo '<input type="hidden" value="'.$resultt['id'].'" id="input_linkcenterr' . $resultt['id'] . '" name="linkcenterr" class="linkcenterr-input">' ;
echo '<input type="button" name="butn" value="Send" onClick="loadInfoo()">';
echo '</select>';
echo '</form>';
}
在函数loadInfoo()
中,应该将$result['id']
作为参数传递,您可以使用该参数按id将元素作为目标
// Pass the id into the function
echo '<input type="button" name="butn" value="Send" onClick="loadInfoo(' . $resultt['id'] . ')">';
只需在循环中实现一个计数器,并将计数器值添加到id中,如下所示:
$i=0;
while($resultt= mysql_fetch_array($find)){
echo '<form id="linkcenter'.$i.'" action="">';
echo '<input type="hidden" value="'.$resultt['id'].'" id="linkcenterr'.$i.'" name="linkcenterr"';
echo '<input type="button" name="butn" value="Send" onClick="loadInfoo('.$i.')">';
echo '</select>';
echo '</form>';
$i++;
}
给
一个class
属性,并使用该属性获取表单。比如说
HTML:
谢谢你,伙计,你那家伙的回答都起作用了。感谢jquery部分,这正是我想要的。
// Pass the id into the function
echo '<input type="button" name="butn" value="Send" onClick="loadInfoo(' . $resultt['id'] . ')">';
function loadInfoo(idVal) {
// Concatenate the id param with onto linkcenterr:
var id = document.getElementById('input_linkcenterr' + idVal).value;
// Or the jQuery version:
var id = $('#input_linkcenterr' + idVal).val();
}
$i=0;
while($resultt= mysql_fetch_array($find)){
echo '<form id="linkcenter'.$i.'" action="">';
echo '<input type="hidden" value="'.$resultt['id'].'" id="linkcenterr'.$i.'" name="linkcenterr"';
echo '<input type="button" name="butn" value="Send" onClick="loadInfoo('.$i.')">';
echo '</select>';
echo '</form>';
$i++;
}
function loadInfoo(index) {
var id = document.getElementById('linkcenterr'+index).value;
//ajax and more java bellow that i know is working..
}
<form class="my_forms">
...
var forms = $(".my_forms");