我已经开发了两个jquery脚本,现在我必须将它们合并在一起,以生成最终的脚本。
这是克隆表单的第一部分。
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);
})
}
});这是我上传图片的第二部分。
// 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也会发生变化。
是台词的问题
$('#show_upload_image_link_button_1').click(function(e) {和
$('#upload_image_link_1').val(attachment.url);需要更改,即#show_upload_image_link_button_1需要为#show_upload_image_link_button_2、3等等
我以前做过一次,现在我不记得我是怎么做的了。
下面是html/php部分,以防万一
$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;发布于 2014-02-26 01:50:36
使用一个类并循环到最后一个元素怎么样?
https://stackoverflow.com/questions/22019305
复制相似问题