Php 要使用JQUERY发布单个选定表单值吗
我正在生成一个键/值数组,并在其中滚动以在表单中生成一组“a”标记。 我希望JQuery检测单击了哪一个“a”标记,并提交表单,以便发布键,而不是值。 JQuery函数在我构建的test.php文件中似乎不起作用Php 要使用JQUERY发布单个选定表单值吗,php,jquery,forms,post,submit,Php,Jquery,Forms,Post,Submit,我正在生成一个键/值数组,并在其中滚动以在表单中生成一组“a”标记。 我希望JQuery检测单击了哪一个“a”标记,并提交表单,以便发布键,而不是值。 JQuery函数在我构建的test.php文件中似乎不起作用 <?php if(isset($_GET['id'])) { echo '<h2>URL parameter passed by $_GET</h2>' ; echo '<pre>' ; echo print_r
<?php if(isset($_GET['id'])) {
echo '<h2>URL parameter passed by $_GET</h2>' ;
echo '<pre>' ;
echo print_r($_GET) ;
echo '</pre>' ;
} elseif(isset($_POST['id'])) {
echo '<h2>URL parameter passed by $_POST</h2>' ;
echo '<pre>' ;
echo print_r($_POST) ;
echo '</pre>' ;
}
?>
<style>
table {
width:50%;
}
td {
width:25%;
border:1pt solid black;
margin:0px;
}
</style>
<?php
// Build Indexed Array
$fruit[0] = 'Orange' ;
$fruit[1] = 'Apple' ;
$fruit[2] = 'Banana' ;
echo '<pre>' ;
echo print_r($fruit) ;
echo '</pre>' ;
// Form definition
echo '<form name="groupform" id="groupform" action="test.php" method="post">' ;
echo '<table>' ;
echo '<tr><td>Array values</td><td>Simple A tag</td><td>Regular submit button</td><td>A tag with class submit</td></tr>' ;
for($key = 0; $key < count($fruit); $key++) {
echo '<tr>' ;
// Array Values
echo '<td>Index is ' . $key . ', value is ' . $fruit[$key] . '</td>' ;
// Simple A tag
echo '<td><a href="?id=' . $key . '">' . $fruit[$key] . '</a></td>' ;
// Regular submit button
echo '<td><input type="submit" name="id" id="id" value="' . $fruit[$key] . '"></td>' ;
// A tag with class submit
echo '<td><a href="" class="submit" name="id" id="id" value="' . $key . '">' . $fruit[$key] . '</a></td>' ;
echo '</tr>' ;
}
echo '<tr><td></td><td>Functions correctly, but don't want to use a URL parameter to send the key</td><td>Can't get this to post the key instead of the value</td><td>Ideally want to style it as an a tag, but post the key</td></tr>' ;
echo '</table>' ;
echo '</form>' ;
?>
<script src="/scripts/jquery-3.3.1.js"></script>
<script>
$(function(){
// when clicking the submit button
$('.submit').click(function(){
// Get the value of the clicked a tag
var usethisID = $(this).val();
alert(usethisID);
// submit the form
// BUT POST the value of usethisID as id
$('#groupform').submit();
});
});
</script>
桌子{
宽度:50%;
}
运输署{
宽度:25%;
边框:1件纯黑;
边际:0px;
}
$(函数(){
//单击“提交”按钮时
$('.submit')。单击(函数(){
//获取单击的标记的值
var usethisID=$(this.val();
警报(usethisID);
//提交表格
//但将usethisID的值作为id发布
$('#groupform')。提交();
});
});
var usethisID=$(this.attr(“value”)
请记住,在DOM中,不能有具有相同“id”的标记!!!
每个id=''必须具有唯一的值!
因此,我建议在每个“id”中构造$key:id\u XXXX
echo';
在jQuery中,从id中提取$key:
$(函数(){
//单击“提交”按钮时
$('.submit')。单击(函数(){
//获取单击的标记的值
var索引=$(this.attr(“id”);
var usethisID=index.substring(index.indexOf(“”)+1);
警报(usethisID);
//提交表格
//但将usethisID的值作为id发布
$('#groupform')。提交();
});
});
很好的延续
_Teddy您没有将jQuery库包含在此代码段中,并且在您明确设置之前,
$
不会被识别为jQuery的引用。谢谢Dan,没有错误!我在JQuery函数之前添加了它,但这似乎并不能解决问题。我认为问题在于函数本身——当然这是在说如果用户单击类为submit的元素,则提交表单。但它不会“读取”实际单击的a标记的值,并使其成为表单提交的标记。我在找对地方了吗?抱歉,它正在将一个值读入usethisID,但它看起来是空的,我还没有指示JQuery将此值作为帖子的一部分发送。谢谢Teddy。这很好,useThisID现在肯定设置正确了。我的函数仍然没有正确地发布值,但我会解决这个问题。。。。
echo '<td><a href="" class="submit" name="id" id="id_'.$key.'" value="' . $key . '">' . $fruit[$key] . '</a></td>' ;
<script>
$(function(){
// when clicking the submit button
$('.submit').click(function(){
// Get the value of the clicked a tag
var index = $(this).attr("id");
var usethisID = index.substring(index.indexOf("_")+1 );
alert(usethisID);
// submit the form
// BUT POST the value of usethisID as id
$('#groupform').submit();
});
});
</script>