首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单击递增div上的事件

单击递增div上的事件
EN

Stack Overflow用户
提问于 2014-02-25 23:49:32
回答 1查看 109关注 0票数 1

我已经开发了两个jquery脚本,现在我必须将它们合并在一起,以生成最终的脚本。

这是克隆表单的第一部分。

代码语言:javascript
复制
function updateClonedInput(index, element) {
    $(element).appendTo("#upload_image_sets").attr("id", "clonedInput" +  index);
    $(element).find(">:first-child").attr("id", "upload_image_link_" + index);
    $(element).find(">:first-child").attr("name", "hero_options[upload_image_link_" + index + "]");
    $(element).find(">:first-child").next().attr("id", "show_upload_image_link_button_" + index);
}

$(document).on("click", ".clone", function(e){
    e.preventDefault();
    var cloneIndex = $(".clonedInput").length + 1;
    var new_Input = $(this).parents(".clonedInput").clone();
    updateClonedInput(cloneIndex, new_Input);   
});
$(document).on("click", "button.remove", function(e){
e.preventDefault();
    if($('.clonedInput').length > 1) {
        $(this).parents(".clonedInput").remove();

        $(".clonedInput").each( function (cloneIndex, clonedElement) {
            updateClonedInput(cloneIndex + 1, clonedElement);
        })
    }

});

这是我上传图片的第二部分。

代码语言:javascript
复制
// Media Upload Button 1
var custom_uploader;
$('#show_upload_image_link_button_1').click(function(e) {
    e.preventDefault();
    //If the uploader object has already been created, reopen the dialog
    if (custom_uploader) {
        custom_uploader.open();
     return;
    }
    //Extend the wp.media object
    custom_uploader = wp.media.frames.file_frame = wp.media({
    title: 'Choose Image',
    button: {
        text: 'Choose Image'
    },
     multiple: false
});
//When a file is selected, grab the URL and set it as the text field's value
custom_uploader.on('select', function() {
    attachment = custom_uploader.state().get('selection').first().toJSON();
    $('#upload_image_link_1').val(attachment.url);
    });
    //Open the uploader dialog
    custom_uploader.open();
});

这适用于第一个表单,但随着表单的递增,div id也会发生变化。

是台词的问题

代码语言:javascript
复制
$('#show_upload_image_link_button_1').click(function(e) {

代码语言:javascript
复制
$('#upload_image_link_1').val(attachment.url);

需要更改,即#show_upload_image_link_button_1需要为#show_upload_image_link_button_2、3等等

我以前做过一次,现在我不记得我是怎么做的了。

下面是html/php部分,以防万一

代码语言:javascript
复制
$hero_options = get_option( 'hero_options' ); 
$count=count($hero_options);
$totalimg=$count-4;
$html = '<div id="upload_image_sets">';
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) { 
for($i=1;$i<=$totalimg;$i++){ 
    $html .= '<div id="clonedInput'.$i.'" class="clonedInput">';
    $html .= '<input type="text" id="upload_image_link_'.$i.'" size="36" name="hero_options[upload_image_link_'.$i.']" value="' . $hero_options['upload_image_link_'.$i.''] . '" />';

    $html .= '<input id="show_upload_image_link_button_'.$i.'" class="button upload_images" type="button" value="Upload Image" />';

    $html .= '<div class="actions">
    <button class="clone">Clone</button> 
    <button class="remove">Remove</button>
</div>
</div>';
}}
$html .= '</div>';

    echo $html;
EN

回答 1

Stack Overflow用户

发布于 2014-02-26 01:50:36

使用一个类并循环到最后一个元素怎么样?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22019305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档