2017-02-09 27 views
-1

我產生無線電輸入一個自定義id這樣的:如何調用動態ID在jQuery選擇

foreach($paymentMethods['CustomPayment'] as $custompay) { 
    $html .= '<input type="radio" id="cnp_payment_method_selection_'.$custompay.'" name="cnp_payment_method_selection" class="cnp_payment_method_selection" style="margin: 0 0 0 0;" value="'.$custompay.'">&nbsp;<b>'.$custompay.'</b>&nbsp;&nbsp;&nbsp;'; 
} 

我現在用的是同一個ID下面的jQuery選擇,但是當它是動態的它不工作:

jQuery("#cnp_payment_method_selection_customay").click(function() { 
    jQuery("#cnp_CreditCard_div").hide();     
    jQuery("#cnp_eCheck_div").hide(); 
    jQuery("#cnp_Custompay_div").show(); 
}); 

如何在該選擇器中調用該動態ID?

在$ custompay我們會得到一些這樣的事定製,CUSTOM1,CUSTOM2基於該IDcnp_payment_method_selection_customay,cnp_payment_method_selection_customay1,cnp_payment_method_selection_customay2

使用下面的代碼

foreach($paymentMethods['CustomPayment'] as $custompay) { 
       $html .='<script type="text/javascript"> 
         var simple = "cnp_payment_method_selection_'.$custompay.'"; 
         alert(simple); 

         </script>'; 

        //print_r($custompayment); 
       $html .= '<input type="radio" id="cnp_payment_method_selection_'.$custompay.'" name="cnp_payment_method_selection" class="cnp_payment_method_selection" style="margin: 0 0 0 0;" value="'.$custompay.'">&nbsp;<b>'.$custompay.'</b>&nbsp;&nbsp;&nbsp;'; 

       } 
       $html .= '</span>'; 

在哪裏我的var簡單是得到我想要的Id,但現在我想通過該變量的地方<input type="radio" id="cnp_payment_method_selection_'.$custompay.'" name="cnp_payment_method_selection" class="cnp_payment_method_selection" style="margin: 0 0 0 0;" value="'.$custompay.'">

可變我要打電話的地點ID的可變

+1

您是否嘗試檢查呈現的HTML的外觀? – eddyP23

+0

時動態添加使用事件代表團 – guradio

+0

我有一個ID,但像#cnp_payment_method_selection_customay,#cnp_payment_method_selection_customay1,#cnp_payment_method_selection_customay2,#cnp_payment_method_selection_customay3一些像這樣的事情,我基於選擇要打到ID –

回答

0

您可以使用「開始於」選擇識別的元素:

$('input[id^=cnp_payment_method_selection_]') 

這將返回所有具有IDS開始元素cnp_payment_method_selection_

https://api.jquery.com/attribute-starts-with-selector/

+0

我不希望所有具有特定變量只我想$ custompay –

0

它們不一樣。

id="cnp_payment_method_selection_'.$custompay.'" 
jQuery("#cnp_payment_method_selection_customay").click(function(){ 

您可以通過數據參數獲取標識。

foreach($paymentMethods['CustomPayment'] as $custompay) { 
        $html .= '<input type="radio" data-id="'.$custompay.'" name="cnp_payment_method_selection" class="cnp_payment_method_selection" style="margin: 0 0 0 0;" value="'.$custompay.'">&nbsp;<b>'.$custompay.'</b>&nbsp;&nbsp;&nbsp;'; 
        } 

jQuery(".cnp_payment_method_selection_customay").click(function(){ 
     var id = $(this).data('id'); 
     }); 
+0

是的,他們是不一樣的,我想要生成的ID在選擇器調用 –

0

您可以使用jQuery.add方法來創建一個動態選擇(您可以遍歷$ paymentMethods [「CustomPayment」]和元素添加到選擇器)

var selector = $();//an empty selector 
foreach($paymentMethods['CustomPayment'] as $custompay) { 
    selector.add('#cnp_payment_method_selection_'.$custompay);//add a $custompay to the selector 
} 
+0

雖然這段代碼可能回答這個問題,提供額外的上下文關於爲什麼和/或如何代碼回答這個問題提高了它的長期價值。 –

+0

你是對的@DonaldDuck,我已經添加了一些背景 –

0

至於我能看到。 我想你試圖用不同的id生成一些inut類型,然後你會將這個動態輸入類型插入到你的任何頁面元素中。

所以,如果你正在生成動態元素並附加到DOM,那麼你不能給他們直接點擊事件,這是行不通的。 爲此,您需要通過父元素引用並需要使用.on。

如果您生成如下所示的元素。

foreach($paymentMethods['CustomPayment'] as $custompay) { 
    $html .= '<input type="radio" id="cnp_payment_method_selection_'.$custompay.'" name="cnp_payment_method_selection" class="cnp_payment_method_selection" style="margin: 0 0 0 0;" value="'.$custompay.'">&nbsp;<b>'.$custompay.'</b>&nbsp;&nbsp;&nbsp;'; 
} 

而讓你附加你的$ HTML來上課容器元素像下面

$(".container").html($html); 

然後你給輸入的點擊事件,你需要給使用。對和父母基準後(這裏是.container),如下所示。

$(".container").on('click','#cnp_payment_method_selection_customay', function() { 
// your stuff here 

}); 
+0

但在這個ID $( 「容器」)。在( '點擊', '#cnp_payment_method_selection_customay',函數(){// 你的東西在這裏 } ); –

+0

其中cnp_payment_method_selection_customay與每個廣播電臺 –

+0

不同,在點擊事件中使用$(this)變量可以獲得$(this).val(); 但我想你想區分點擊事件的基礎上,那麼你不需要設置不同的ID。只需使用您的課程,例如 並使用 $(「。container」)。點擊','cnp_payment_method_selection',function(){ //你的東西 var clicked_rd_value = $(this).val(); }); 使用點擊無線電的價值,你可以任何你想要的。 –