簡體   English   中英

在javascript的幫助下增加php變量?

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

我有一個帶有輸入文本框的表單和一個用於添加另一個輸入文本框的鏈接。

第一個輸入文本框的標簽為 Artist1。 當我單擊鏈接添加另一個輸入文本框時,我希望標簽是 Artist2,如果我再次單擊 Artist3,依此類推。

我有以下鏈接:

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

當我點擊鏈接時,它會執行這個 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;
    });    
});

@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