简体   繁体   English

在javascript的帮助下增加php变量?

[英]Increment php variable with the help of javascript?

I have a form with an input textbox and a link to add another input textbox.我有一个带有输入文本框的表单和一个用于添加另一个输入文本框的链接。

The first input textbox has the label Artist1.第一个输入文本框的标签为 Artist1。 When I click the link to add another input textbox, I want the label to be Artist2, if I click again Artist3 and so on.当我单击链接添加另一个输入文本框时,我希望标签是 Artist2,如果我再次单击 Artist3,依此类推。

I have the following link:我有以下链接:

<a href="#" id="addScntHeadliners">Add another</a>

and as I click on the link it executes this javascript function:当我点击链接时,它会执行这个 javascript 函数:

<script type="text/javascript">
$(function() {
    var scntDiv = $('#p_scentsHeadliners');
    var i = $('#p_scentsHeadliners').size() + 1;

    $('#addScntHeadliners').bind('click', function() {
        //$().appendTo(scntDiv);
        $('<p id="newp'+i+'"></p>').appendTo(scntDiv);
        var html = '<br /><?php echo $this->Form->input('Artist'.$qtd_headliners,['class="form-control" rows="7" placeholder="e.g. Day 1" style="max-width: 150px; max-height: 200px;"']); ?><br /><label for="formGroupExampleInput2">Photo</label><div class="fileupload fileupload-new" data-provides="fileupload"><span class="btn btn-primary btn-file"><span class="fileupload-new">Select file</span><span class="fileupload-exists">Change</span><input type="file" name="file"/></span><span class="fileupload-preview"></span><a href="#" class="close fileupload-exists" data-dismiss="fileupload" style="float: none">×</a></div><a href="#" id="remScntHeadliners' + i +'">Remove</a>';
        var succss = $("#newp"+i).append(html)
        $('#remScntHeadliners'+ i ).on('click', function() {
            $(this).parent().remove();
            return false;
        });
        i++;
        return false;
    });    
});

The solution as sugested by @madalinivascu was to go full js! @madalinivascu 提出的解决方案是使用完整的 js! :) :)

<script type="text/javascript">
$(function() {
    var scntDiv = $('#p_scentsHeadliners');
    var i = $('#p_scentsHeadliners').size() + 1;
    var headlinersqtd = <?php echo $qtd_headliners; ?>;
    $('#addScntHeadliners').bind('click', function() {
        //$().appendTo(scntDiv);
        $('<p id="newp'+i+'"></p>').appendTo(scntDiv);
        headlinersqtd++;
        var html = '<br /><label for="artist1">Artist '+ headlinersqtd+'</label><input type="text" name="Artist'+headlinersqtd+'" class="form-control" rows="7" placeholder="e.g. Day 1" style="max-width: 150px; max-height: 200px;" ="class="form-control" "="" id="Artist'+headlinersqtd+'"><br /><label for="formGroupExampleInput2">Photo</label><div class="fileupload fileupload-new" data-provides="fileupload"><span class="btn btn-primary btn-file"><span class="fileupload-new">Select file</span><span class="fileupload-exists">Change</span><input type="file" name="file"/></span><span class="fileupload-preview"></span><a href="#" class="close fileupload-exists" data-dismiss="fileupload" style="float: none">×</a></div><a href="#" id="remScntHeadliners' + i +'">Remove</a>';
        var succss = $("#newp"+i).append(html)
        $('#remScntHeadliners'+ i ).on('click', function() {
            $(this).parent().remove();
            return false;
        });
        i++;
        return false;
    });    
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM