简体   繁体   English

我在调用 javascript 时做错了什么?

[英]What am I doing wrong in this call to javascript?

I have this in html我在 html 中有这个

 <div class="btn-group tags_select"> <a href='' id='<?php echo "plus".$base['id']?>' class='btn btn-flat btn-primary btn-sm'><i class='fa fa-plus'></i><?php if(in_array("manageAsset",$perms)) { echo $base['id']; }?></a> <a href='' id='<?php echo "minus".$asset['id']?>' class='btn btn-flat btn-success btn-sm disabled'><i class='fa fa-minus'></i><?php if(in_array("manageAsset",$perms)) { echo $asset['id']; }?></a> </div>

this is the javascript:这是javascript:

 <script type="text/javascript" language="javascript"> var array = []; var len; $('.tags_select a').click(function() { var value = $(this).text(); var input = $('#asset_array'); if($('plus'+value).click()){ console.log("clicked plus"); document.getElementById('plus'+value).classList.add('disabled') ; document.getElementById('minus'+value).classList.remove('disabled'); if(!array.includes(value)){ array.push(value); console.log(array); input.val(array.toString()); } } else if($('minus'+value).click()){ console.log("clicked minus"); document.getElementById('minus'+value).classList.add('disabled') ; document.getElementById('plus'+value).classList.remove('disabled'); if(array.includes(value)){ array.pop(value); console.log(array); input.val(array.toString()); } } return false; }); </script>

There are 2 buttons, minus is turned off while the plus is on.有2个按钮,加号打开时减号关闭。 When i click on plus the minus should be on and plus off ( as it is), but the output for minus gives me only log for ('clicked plus').当我点击加号时,减号应该打开和加号关闭(原样),但减号的输出只给我登录(“点击加号”)。 Does this mean i need separate id calls in document?这是否意味着我需要在文档中单独调用 id?

在此处输入图片说明

The syntax for your conditional won't work here.您的条件语法在这里不起作用。 You're not going to be able to check whether the link has been clicked by checking if($('plus'+value).click()){ because your conditional is effectively checking the returned value of $('plus'+value).click() and JQuery's click() method returns nothing/null.您将无法通过检查if($('plus'+value).click()){来检查链接是否已被点击,因为您的条件有效地检查了$('plus'+value).click()和 JQuery 的click()方法不返回任何内容/null。

What you would be better off checking is the id of the target of the click event.您最好检查click事件目标的 id。 So firstly you would have to include the event as an argument being passed to the click handler like so:因此,首先您必须将事件作为参数传递给点击处理程序,如下所示:

$('.tags_select a').click(function(evt) {

The argument there has information you can access about the event's target, which in this case is the anchor <a> tag that has been clicked.那里的参数包含您可以访问的有关事件目标的信息,在本例中是已单击的锚点<a>标记。

So what you could check in this case would be the evt.target.id which would contain either "plus" or "minus":所以在这种情况下你可以检查的是evt.target.id ,它包含“plus”或“minus”:

if(evt.target.id.includes("plus")) {
  //proceed with logic for "plus" link having been clicked
} else if(evt.target.id.includes("minus")){
  //proceed with logic for "minus" link having been clicked
}

Mind you, the includes() method I'm using here is vanilla javascript and JQuery may have a call that does the same thing.请注意,我在这里使用的includes()方法是普通的 javascript,而 JQuery 可能有一个执行相同操作的调用。 No harm in mixing the two though.不过把两者混合起来也没什么坏处。

You can find more information about event.target here if you like: https://developer.mozilla.org/en-US/docs/Web/API/Event/target如果您愿意,可以在此处找到有关event.target更多信息: https : //developer.mozilla.org/en-US/docs/Web/API/Event/target

当您调用$('minus'+value).click()您正在触发一个点击事件,而不是评估该元素是否被点击。

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

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