我有一个函数,用于创建文本输入集:
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;
});
});
如你所见,在每个输入的'name'字段中都添加了一个增加值。它看起来像:
name="test_testbutton[0][0]", name="test_testbutton[1][0]", name="test_testbutton[2][0]", etc.
该功能可以根据需要删除选定的输入。它与一些PHP代码连接(不管代码是什么样子),这些代码将所有内容保存并存储为数组。Array创建后,将显示保存的输入。它们在'name'字段中的数字与之前由上述函数创建的字段中的数字相同。所以它看起来像:
<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'字段的最大值,并创建另一个具有更高值的输入,例如:
如果输入的'name'的最大值是'name="test_testbutton[25][0] ",点击'Add'后,它会创建'name="test_testbutton[26][0] ",以此类推…
现在! . .我的问题是,见鬼!!如何实现?
编辑!
整个PHP代码是基于表单提交。我想我没有提供足够的PHP代码的细节相对于我的jQuery函数,所以它是:
与我上面提供的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>
和一个在提交时将所有内容保存在数据库中的函数:
function theme_settings_page() {
global $themename,$theme_options;
$i=0;
$message='';
if ( 'save' == $_REQUEST['action'] ) {
foreach ($theme_options as $value) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
foreach ($theme_options as $value) {
if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ] ); } else { delete_option( $value['id'] ); } }
$message='saved';
}
else if( 'reset' == $_REQUEST['action'] ) {
foreach ($theme_options as $value) {
delete_option( $value['id'] ); }
$message='reset';
}
if ( $message=='saved' && $i==0 ) echo '<div id="message" class="apply"><strong>Saved.</strong></div>' ;
if ( $message=='reset' ) echo '<div id="message" class="reset"><strong>Reset.</strong></div>';
?>
所以再一次,我需要一个函数,通过搜索所有的'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;
});
});
这个解决方案需要这个插件
如果您正在通过ajax调用运行保存/删除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
}
}
很抱歉打扰你们,我真的很抱歉(我没有给你们足够的细节来正确回答我的问题:()!我自己找到了一个无缝的解决方案。我所做的是将整个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;
});
});
现在,每次我点击"添加",它寻找最高的键值,并添加一个输入文件与键+ 1值:)