2011-12-02 26 views
0

我有這些jQuery標籤,通過ajax拉內容時點擊,但由於某種原因,似乎沒有任何東西工作,我有很多東西在裏面。jQuery沒有JavaScript工作內部的Ajax創建標籤

<li><a href="page.php">Page</a></li> 

和頁面有一堆的元素在裏面suchly

<form id="create_upload_image_form_dialog_<?php echo $u_row['type']; ?>" onsubmit="return false;" method="post" action="<?php echo $urlbase; ?>/build/scripts/upload" enctype="multipart/form-data"> 
     <input onchange="create_upload_image_dialog('#create_upload_image_form_dialog_<?php echo $u_row['type']; ?>','<?php echo $u_row['type']; ?>','<?php echo $urlbase; ?>/themes/<?php echo $_SESSION['theme_root']; ?>/Library/Themes/<?php echo str_replace('+', '%20', urlencode($_SESSION['theme_name'])); ?>.theme/<?php echo $u_row['image_path'] . $u_row['image_name']; ?>.png');" type="file" name="imgfile" id="create_image_upload_<?php echo $u_row['type']; ?>" />   
     <input type="hidden" value="image_style_scale" name="image_style" id="image_style_dialog_<?php echo $u_row['type']; ?>"/> 
     <input type="hidden" name="image_type" value="<?php echo $u_row['type']; ?>" /> 
     <input type="hidden" name="image_negative" value="" id="image_negative_dialog_<?php echo $u_row['type']; ?>"/> 
     <input type="hidden" name="image_grayscale" value="" id="image_grayscale_dialog_<?php echo $u_row['type']; ?>"/> 
    </form> 
    Optimal image size is <?php echo $u_row['image_x'] * 2; ?>x<?php echo $u_row['image_y'] * 2; ?>. 
</span>   
<span><form id="create_upload_image_form_<?php echo $u_row['type']; ?>" onsubmit="create_upload_image(this,'<?php echo $u_row['type']; ?>','<?php echo $urlbase; ?>/themes/<?php echo $_SESSION['theme_root']; ?>/Library/Themes/<?php echo str_replace('+', '%20', urlencode($_SESSION['theme_name'])); ?>.theme/<?php echo $u_row['image_path'] . $u_row['image_name']; ?>.png');return false;" method="post" action="<?php echo $urlbase; ?>/build/scripts/upload" enctype="multipart/form-data"> 
    <?php if (false/* strlen($u_row['image_default_path']) > 0 */) { ?><span><input type="button" value="Download original image" onClick="download_image();"/></span><?php } ?> 
     <span class="resize_method"> 
      <button onclick="create_reset_image('<?php echo $u_row['type']; ?>','<?php echo $default; ?>');" >Reset</button> 
      <button onclick="$('#upload_image_dialog_<?php echo $u_row['type']; ?>').dialog('open');return false;" id="create_upload_button-<?php echo $u_row['type']; ?>">Upload</button> 
      <input onchange="create_update_elements('<?php echo $u_row['type']; ?>',this);" type="radio" name="image_style_<?php echo $u_row['type']; ?>" id="image_style_scale_<?php echo $u_row['type']; ?>" value="image_style_scale" checked="checked"/><label for="image_style_scale_<?php echo $u_row['type']; ?>">Scale</label> 
      <input onchange="create_update_elements('<?php echo $u_row['type']; ?>',this);" type="radio" name="image_style_<?php echo $u_row['type']; ?>" id="image_style_stretch_<?php echo $u_row['type']; ?>" value="image_style_stretch"/><label for="image_style_stretch_<?php echo $u_row['type']; ?>">Stretch</label> 
      <input onchange="create_update_elements('<?php echo $u_row['type']; ?>',this);" type="radio" name="image_style_<?php echo $u_row['type']; ?>" id="image_style_zoom_<?php echo $u_row['type']; ?>" value="image_style_zoom"/><label for="image_style_zoom_<?php echo $u_row['type']; ?>">Zoom</label> 
      <input onchange="create_update_elements('<?php echo $u_row['type']; ?>',$('#image_style_<?php echo $u_row['type']; ?>'));" type="checkbox" name="image_negative" id="image_negative_<?php echo $u_row['type']; ?>" value="true"/><label for="image_negative_<?php echo $u_row['type']; ?>">Negative</label> 
      <input onchange="create_update_elements('<?php echo $u_row['type']; ?>',$('#image_style_<?php echo $u_row['type']; ?>'));" type="checkbox" name="image_grayscale" id="image_grayscale_<?php echo $u_row['type']; ?>" value="true"/><label for="image_grayscale_<?php echo $u_row['type']; ?>">Grayscale</label> 
      <button id="okay_button_<?php echo $u_row['type']; ?>" onclick="$('create_upload_image_form_<?php echo $u_row['type']; ?>').submit();">Save</button> 
     </span> 
     <br /> 
     <span style="display:none;"> 
      <label for="image_background_color_<?php echo $u_row['type']; ?>">Background color</label><input type="text" name="image_background_color" id="image_background_color_<?php echo $u_row['type']; ?>"/> 
      <label for="image_tint_color_<?php echo $u_row['type']; ?>">Tint color</label><input type="text" name="image_tint_color" id="image_tint_color_<?php echo $u_row['type']; ?>"/> 
     </span> 
     <input type="hidden" name="image_type" value="<?php echo $u_row['type']; ?>" /> 
     <input type="hidden" id="image_style_<?php echo $u_row['type']; ?>" name="image_style" value="image_style_scale" /> 
    </form></span><br /> 

而且我有jQuery用戶界面使得這些元素看起來真的很不錯,我在這裏裏面的一些其他UI的東西太多,唐」工作。這裏的JS

$('#create_theme').tabs({ 
     ajaxOptions: { 
      error: function(xhr, status, index, anchor) { 
       $(anchor.hash).html(
        "Couldn't load this tab. We'll try to fix this as soon as possible. "); 
      } 
     } 
    }); 
    $('.tab_images').accordion({ 
     autoHeight: false, 
     header: 'h3' 
    }); 
    $(".upload_image_dialog").dialog({ 
     autoOpen: false 
    }); 
    $("input[type='button']").button(); 
    $('.resize_method').buttonset(); 
     $('.presets').hover(function(){ 
     $('.presets').css('overflow-y','auto'); 
    },function(){ 
     $('.presets').css('overflow-y','hidden'); 
    }); 

基本上,當它從一開始它的工作原理的存在,但任何在通過Ajax拉不。我已經看到了一些與$ .live()(1.7中的$ .on()不贊成使用)有關的事情,這可能會幫助我,但我真的很失落。

+0

您必須使用$ .live()(或$ .on()),正如您在帖子中提到的,將事件綁定到動態生成或更改的元素。你爲什麼沒有嘗試?這個jQuery代碼中的ajax生成的元素是什麼? –

+0

很多'

回答

1

我會傾向委託此處將代碼從標記中取出。代表在現場的優點是現場結合的身體和委託(在這個例子中)結合到.resize_method歸類元素:

<span class="resize_method"> 

舊標記剪斷:

<button onclick="create_reset_image('<?php echo $u_row['type']; ?>','<?php echo $default; ?>');" >Reset</button> 

改變的標記(我添加一個ID按鈕,但你可以做其他的選擇,以及如果你願意的話):

<button id='myReset'>Reset</button> 

新代碼:(把其他的東西的文檔準備事件的內部以及)

$(document).ready(function(){ 
    $('.resize_method).delegate('#myReset', 'click', function(){ 
     create_reset_image('<?php echo $u_row['type']; ?>','<?php echo $default; ?>');" 
    }); 
}); 
+0

啊,不,我看到這部分jQuery的工作原理!謝謝你噸,順便說一句,我愛如何stackoverflow認爲你的JavaScript是PHP和一切灰色過去你的hashtag –

1

爲什麼不把那些jQuery文檔就緒方法中的那些HTML元素的ON EVENT方法(onclick,onchange屬性中定義的方法)與您已經放在那裏的方法類似?它很難保持它,因爲你已經把它直接放在html元素中。我相信你會明白如何使用$ .live,如果你這樣做。
讓說你把「my_reset_button」作爲第一個按鈕(這是與價值「復位」)

<button id="my_reset_button" name="my_reset_button" type="button">Reset</button> 

裏面的jQuery文檔準備功能的ID,

$('#my_reset_button').live('click', function(){ 

    create_reset_image('<?php echo $u_row['type']; ?>','<?php echo $default; ?>'); 

);