dongwei2882 2014-03-19 21:37
浏览 18
已采纳

如何从文本输入名称中选择最高值并添加具有更高值的另一个字段?

I have a function, which createssets of text inputs:

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;
    });
});

as you can see it puts in every input's 'name' field a incremented value. It looks like:

name="test_testbutton[0][0]",  name="test_testbutton[1][0]", name="test_testbutton[2][0]", etc.

The function can remove selected input on demand. It is connected with some PHP code (doesn't matter what the code look like), which saves everything and stores as an array. When Array is created the saved inputs are displayed. They have the same numbers in 'name' field as previously cerated field by above function. so it looks like:

<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="" /></>

and so on......

But the problem appears when, after saving, I want to add another text inputs. My jQuery function starts counting from 0. I'd like it to find a highest value of every 'name' fields and create another inputs with higher value, for example:

if the 'name' of input with highest value is 'name="test_testbutton[25][0]' after clcking 'Add' it creates 'name="test_testbutton[26][0]', and so on...

And now!.. my question, badam tsss!!: how to achieve it?


EDIT!

The whole PHP code is based on form submitting. I think I didn't give enough details about PHP code relative to my jQuery function, so there it is:

Function directly relative to jQuery I provided above, it stores inputs texts as an array 'test_testbutton', thanks to code presented below everything is displayed back as a inputs sets:

<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>

And a function which saves everything in database on submit:

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>';

?>

So again, I need a function which searches through all 'test_testbutton['+a+'][0]' values on website after they being saved on submit, and when I click Add button it puts in place of ['+a+'] 'highest value +1'.

  • 写回答

3条回答 默认 最新

  • dongxing7318 2014-03-22 12:58
    关注

    Sorry for bothering you guys, I really am (I didn't give you enough details to answer my question properly:()! I found a seamless solution by myself. What i did is to move the whole jQuery code into a PHP file, then I found a highest array's key value and usedin a jQuery loop, here it is:

    <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;
        });
    });
    

    now, everytime I click 'Add' It looks for higest key value and add an input filed with Key + 1 value :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度