Php 如何从文本输入名称中选择最高值,并添加另一个值较高的字段?
我有一个函数,可以创建文本输入集:Php 如何从文本输入名称中选择最高值,并添加另一个值较高的字段?,php,jquery,Php,Jquery,我有一个函数,可以创建文本输入集: jQuery(function($) { var scntDiv = $('#textfields'); var i = $('#textfields p').size() + 2; var a = 0; $('#addbutton').click (function() { $('<p><input type="text" id="test_testbutton" name="test_tes
jQuery(function($) {
var scntDiv = $('#textfields');
var i = $('#textfields p').size() + 2;
var a = 0;
$('#addbutton').click (function() {
$('<p><input type="text" id="test_testbutton" name="test_testbutton['+a+'][0]" value="" placeholder="Input Value" /><input type="text" id="test_testbutton" name="test_testbutton['+a+'][1]" value="" placeholder="Input Value" /><a href="#" id="removebutton">Remove</a></p>').appendTo(scntDiv);
i++;
a++;
return false;
});
$('#removebutton').live('click',function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
该功能可根据需要删除选定的输入。它与一些PHP代码连接(不管代码是什么样子),这些代码保存所有内容并存储为一个数组。创建阵列时,将显示保存的输入。它们在“名称”字段中的编号与之前通过上述函数设置的字段中的编号相同。所以它看起来像:
<p><input type="text" id="" name="test_testbutton[0][0]" value="" placeholder="" />
<input type="text" id="" name="test_testbutton[0][1]" value="" placeholder="" /></p>
<p><input type="text" id="" name="test_testbutton[1][0]" value="" placeholder="" />
<input type="text" id="" name="test_testbutton[1][1]" value="" placeholder="" /></>
等等
但当保存后,我想添加另一个文本输入时,问题就出现了。我的jQuery函数从0开始计数。我希望它能找到每个“名称”字段的最高值,并创建另一个值更高的输入,例如:
如果输入的最大值的“名称”在单击“添加”后为“name=”test_testbutton[25][0]”,则会创建“name=”test_testbutton[26][0],依此类推
现在!。。我的问题,badam tsss!!:如何实现
编辑强> 整个PHP代码都基于表单提交。我想我没有给出与jQuery函数相关的PHP代码的足够详细信息,因此:
<input name="save" class="addbutton button" type="submit" value="+" />
<div id="textfields">
<?php
if (isset($_POST[$value['id']]) && is_array($_POST[$value['id']])) {
$_POST[$value['id']];}
$textfields = get_option($value['id']);
if (!empty($textfields)) {
foreach ($textfields as $key => $textfield):
?><p><input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id'];?>[<?php echo $key;?>][0]" value="<?php echo $textfield[0];?>" placeholder="Input Value" /><input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id']; ?>[<?php echo $key;?>][1]" value="<?php echo $textfield[1];?>" placeholder="Input Value" /><a href="#" id="removebutton">Remove</a></p>
<?php endforeach;
$max = max(array_keys($textfields))+1;}
else {
$max = 0;}
?>
</div>
<script type="text/javascript">
jQuery(function($) {
var scntDiv = $('#textfields');
var i = $('#textfields p').size() + 2;
var x = <?php echo $max;?>;
var a = x;
$('.addbutton').click (function() {
$('<p><input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id']; ?>['+a+'][0]" value="" placeholder="Input Value" /><input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id']; ?>['+a+'][1]" value="" placeholder="Input Value" /><a href="#" id="removebutton">Remove</a></p>').appendTo(scntDiv);
i++;
a++;
return false;
});
$('#removebutton').live('click',function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
函数与上面提供的jQuery直接相关,它将输入文本存储为一个数组“test_testbutton”,感谢下面提供的代码,所有内容都显示为输入集:
<a href="#" id="addbutton">Add</a>
<div id="textfields">
<?php
if (isset($_POST['test_testbutton']) && is_array($_POST['test_testbutton'])) {
$_POST['test_testbutton'];}
$textfields = get_option('test_testbutton');
if (!empty($textfields)) {
foreach ($textfields as $key => $textfield):?>
<p><input type="text" id="test_testbutton" name="test_testbutton[<?php echo $key;?>][0]" value="<?php echo $textfield[0];?>" placeholder="Input Value" /><input type="text" id="test_testbutton" name="test_testbutton[<?php echo $key;?>][1]" value="<?php echo $textfield[1];?>" placeholder="Input Value" /><a href="#" id="removebutton">Remove</a></p><?php
endforeach;}
?>
</div>
因此,我再次需要一个函数,在提交时保存所有“test_testbutton['+a+'][0]”值后,搜索网站上的所有“test_testbutton['+a+'][0]”值,当我单击“添加”按钮时,它会将['+a+']'最高值+1'放在位置。使用cookies在重新加载时保存
a
值:
jQuery(function($) {
var scntDiv = $('#textfields');
var i = $('#textfields p').size() + 2;
var a = $.cookie("testButtonCount");;
$('#addbutton').click (function() {
$('<p><input type="text" id="test_testbutton" name="test_testbutton['+a+'][0]" value="" placeholder="Input Value" /><input type="text" id="test_testbutton" name="test_testbutton['+a+'][1]" value="" placeholder="Input Value" /><a href="#" id="removebutton">Remove</a></p>').appendTo(scntDiv);
i++;
a++;
$.cookie("testButtonCount", a);
return false;
});
$('#removebutton').live('click',function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
jQuery(函数($){
var scntDiv=$(“#textfields”);
变量i=$('#textfields p').size()+2;
var a=$.cookie(“testButtonCount”);;
$(“#添加按钮”)。单击(函数(){
$('p>')。附录(scntDiv);
i++;
a++;
$.cookie(“testButtonCount”,a);
返回false;
});
$(“#removebutton”).live('click',function(){
如果(i>2){
$(this.parents('p').remove();
我--;
}
返回false;
});
});
此解决方案需要。如果通过ajax调用运行save/remove php,则可以将计数(_a)保存在DOM变量中。将ID放在容器元素上,这样可以更容易地删除它们
var _a = 0;
jQuery(function($) {
var scntDiv = $('#textfields');
$('#addbutton').click (function() {
$('<p id="p'+_a+'"><input type="text" id="test_testbutton" name="test_testbutton['+_a+'][0]" value="" placeholder="Input Value" /><input type="text" id="test_testbutton" name="test_testbutton['+_a+'][1]" value="" placeholder="Input Value" /><a href="#" onclick="removeRow('+_a+');return false;">Remove</a></p>').appendTo(scntDiv);
_a++;
$.post('save.php', {id:_a});
return false;
});
});
function removeRow(id){
if( $('#textfields p').size() > 2 ) {
$.post('remove.php',{id: id});
$('#p'+id).remove();
// don't decrease _a on the remove. Keeps your ids unique
}
}
var\u a=0;
jQuery(函数($){
var scntDiv=$(“#textfields”);
$(“#添加按钮”)。单击(函数(){
$('p id=“p'+\u a+'”>')。附录(scntDiv);
_a++;
$.post('save.php',{id:_a});
返回false;
});
});
函数(id){
如果($('#textfields p').size()>2){
$.post('remove.php',{id:id});
$('#p'+id).remove();
//不要减少删除上的_a。保持您的ID唯一
}
}
很抱歉打扰你们,我真的很抱歉(我没有给你们足够的细节来正确回答我的问题:()!我自己找到了一个无缝的解决方案。我所做的是将整个jQuery代码移动到一个PHP文件中,然后我找到了一个最高数组的键值并在jQuery循环中使用,如下所示:
<input name="save" class="addbutton button" type="submit" value="+" />
<div id="textfields">
<?php
if (isset($_POST[$value['id']]) && is_array($_POST[$value['id']])) {
$_POST[$value['id']];}
$textfields = get_option($value['id']);
if (!empty($textfields)) {
foreach ($textfields as $key => $textfield):
?><p><input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id'];?>[<?php echo $key;?>][0]" value="<?php echo $textfield[0];?>" placeholder="Input Value" /><input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id']; ?>[<?php echo $key;?>][1]" value="<?php echo $textfield[1];?>" placeholder="Input Value" /><a href="#" id="removebutton">Remove</a></p>
<?php endforeach;
$max = max(array_keys($textfields))+1;}
else {
$max = 0;}
?>
</div>
<script type="text/javascript">
jQuery(function($) {
var scntDiv = $('#textfields');
var i = $('#textfields p').size() + 2;
var x = <?php echo $max;?>;
var a = x;
$('.addbutton').click (function() {
$('<p><input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id']; ?>['+a+'][0]" value="" placeholder="Input Value" /><input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id']; ?>['+a+'][1]" value="" placeholder="Input Value" /><a href="#" id="removebutton">Remove</a></p>').appendTo(scntDiv);
i++;
a++;
return false;
});
$('#removebutton').live('click',function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
我下载了插件并安装了它。我使用了你的函数,但它不起作用。当我添加下一个输入行时,我收到的不是后续的数字,而是NaN。或者我不知道如何正确使用它……忘了提一下。在PHP代码中,你需要添加条件循环生成
(通过迭代到testButtonCount
count)如果设置了testButtonCount
,则字段。可能会有帮助。我忘记添加“save”函数代码,您的示例不起作用,因为我没有提供足够的详细信息。请参阅我问题中的编辑。除非您有一个save.php和一个remove.php来处理您的请求。我只是展示了一个如何添加/删除它的示例ms,以及将服务器端处理程序放在何处。您指出,问题是计数器被重置为零。通过ajax使用静默post,您可以增加/减少服务器端变量,并使其与客户端值保持同步。如果刷新页面或回发邮件,它将被设置为0。您可以随时这样做一旦所有框都设置好了,一篇完整的文章。你的php页面就有了从你的文章中获取的所有信息。我猜你在WordPress中做了什么?是的,是WordPress。很抱歉打扰你,但我自己发现了一个问题,这需要一点费劲的思考,因为我是php/jQuery方面的新手。我将整个jQuery代码移到了php文件中,我停止了Red是一个最高的ARAY的键值和UT进入上面的jQuery Funcot。工作就像一个魅力!不用担心。如果它工作得很好。WP是一个伟大的平台,但是在尝试超越它自己的处理请求的方式时确实有一些限制要考虑。
<input name="save" class="addbutton button" type="submit" value="+" />
<div id="textfields">
<?php
if (isset($_POST[$value['id']]) && is_array($_POST[$value['id']])) {
$_POST[$value['id']];}
$textfields = get_option($value['id']);
if (!empty($textfields)) {
foreach ($textfields as $key => $textfield):
?><p><input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id'];?>[<?php echo $key;?>][0]" value="<?php echo $textfield[0];?>" placeholder="Input Value" /><input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id']; ?>[<?php echo $key;?>][1]" value="<?php echo $textfield[1];?>" placeholder="Input Value" /><a href="#" id="removebutton">Remove</a></p>
<?php endforeach;
$max = max(array_keys($textfields))+1;}
else {
$max = 0;}
?>
</div>
<script type="text/javascript">
jQuery(function($) {
var scntDiv = $('#textfields');
var i = $('#textfields p').size() + 2;
var x = <?php echo $max;?>;
var a = x;
$('.addbutton').click (function() {
$('<p><input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id']; ?>['+a+'][0]" value="" placeholder="Input Value" /><input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id']; ?>['+a+'][1]" value="" placeholder="Input Value" /><a href="#" id="removebutton">Remove</a></p>').appendTo(scntDiv);
i++;
a++;
return false;
});
$('#removebutton').live('click',function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});