如何在jquery选择器中调用动态id

时间:2017-02-09 10:43:20

标签: javascript jquery

我使用自定义id生成无线电输入,如下所示:

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

我在jQuery选择器中使用下面相同的ID但是当它动态时它不起作用:

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 的内容,基于该ID应该是 cnp_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作为变量

调用

4 个答案:

答案 0 :(得分:0)

您可以使用'starts with'选择器来识别元素:

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

这将返回所有包含以cnp_payment_method_selection_开头的ID的元素。

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

答案 1 :(得分:0)

他们不一样。

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

您可以使用数据参数获取ID。

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

答案 2 :(得分:0)

您可以使用jQuery.add方法创建动态选择器(您可以遍历$ paymentMethods [&#39; CustomPayment&#39;]并将元素添加到选择器中)

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

答案 3 :(得分: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);

然后,您将使用.on进行点击事件输入,并从父母(此处.container)引用,如下所示。

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

});
相关问题