duanchongzi9997 2014-02-25 15:49 采纳率: 0%
浏览 34

在递增div时单击事件

I've got 2 jquery scripts i've been working on, I now have to merge them together to make the final script.

Here's the first part which clones the form.

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

});

Here's the second part that allows me to upload an image.

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

This works for the first form, but as the form increments the div id changes.

It's the lines

$('#show_upload_image_link_button_1').click(function(e) {

and

$('#upload_image_link_1').val(attachment.url);

that need changing i.e. #show_upload_image_link_button_1 needs to be #show_upload_image_link_button_2, 3 and so on

I've done this once before and now I can't remember how I done it.

Here's the html/php part just incase

$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;
  • 写回答

1条回答 默认 最新

  • douqian9729 2014-02-25 17:50
    关注

    How about using a class and just Looping to the last element?

    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)