简体   繁体   English

我有一个使用ajax调用在另一个页面的div标签中加载的html页面,我想在加载的页面上触发javascript函数吗?

[英]I have a html page that loaded in div tag in another page using ajax call I want to trigger javascript functions on loaded page?

The main problem is the loaded html page contain loop of cards each card has checkbox I need trigger javascript function that check if checkbox selected then change text value in card to from select to selected, But the function I wrote to trigger this checkboxes not work? 主要问题是加载的html页面包含卡循环每个卡都有复选框我需要触发javascript函数,该功能检查是否选中了复选框,然后将卡中的文本值更改为select到selected,但是我编写的用于触发此复选框的函数不起作用? My checkbox tag from the appended html page: 我的复选框标记来自附加的html页面:

<div class="body-action-button u-flex-center">
      <div><input type="checkbox" style="float: left;" name="companies[]" value="<?php echo $company->id; ?>" id="cbtest<?php echo $company->id; ?>" />
        Request an offer</div>
</div>

javascript function I used to trigger checkbox : 我用来触发复选框的javascript函数:

$('.body-action-button').click(function(){
        alert('selected');//This alert for testing
    });

Try this ( fiddle ): JS 试试这个( 小提琴 ):JS

$('input:checkbox[name=example]').each(function() {
  if (this.checked) {
    alert("selected");
  }
  // else {}
});

HTML HTML

<input type="checkbox" name="example" /> label one
<input type="checkbox" name="example" checked/> label two

You can try to give a class to your checkboxes like 您可以尝试给您的复选框一个类,例如

<input type="checkbox" class="cbxTest" style="float: left;" name="companies[]" value="<?php echo $company->id; ?>" id="cbtest<?php echo $company->id; ?>" />

and add your event to JQuery like that: 并将事件添加到JQuery中,如下所示:

$(document).ready(function () {
    $(".cbxTest").click(function () {
        if (this.checked) {
            alert("Selected");
        }
    });    
});

Also if you want to not trigger it on click but only when checked you can also use .check instead of .click like that 此外,如果你想不触发它的点击,但只有当检查你也可以使用.check代替.click一样,

$(document).ready(function () {
    $(".cbxTest").change(function () {
        if (this.checked) {
            alert("Selected");
        }
    });    
});

Both works well. 两者都很好。 It's just about the scenerio... 只是关于场景...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM