简体   繁体   English

在javascript函数中发送此参数

[英]Sending this parameter in javascript function

I have checkbox as below : 我有以下复选框:

<label class="checkbox"><input type="checkbox" class="serviceable_check_1" id="serviceable1" name="condition_of_materials[]" onclick="checkOnlyOne(1,obj); return false;" ><span> Serviceable</span></label>

And javascript function is : 而javascript函数是:

function checkOnlyOne(slno, this) { //Error here 
  console.log(this);
  //$('.serviceable_check_1').not(this).prop('checked', false);
  if(this.checked){
     $('.serviceable_check_1').not(this).prop('checked', false);
  }
}

I am getting javascript error as Uncaught SyntaxError: Unexpected token this . 我收到JavaScript错误,原因是Uncaught SyntaxError: Unexpected token this How do I send this parameter onchange event ? 如何发送此参数onchange事件?

Your this does not refer to anything. 您的这个没有任何意义。 You can't put this on a function unless it is an object or part of the DOM. 除非它是DOM的对象或一部分,否则不能将放在函数上。

You can put the this keyword on the element that will trigger the function as an argument. 您可以将this关键字放在将触发函数作为参数的元素上。 Then on your function, make the parameter a variable like chckbx or radiobtn if those are your elements 然后在函数上,将参数设为chckbx或radiobtn之类的变量(如果这些是您的元素)

document.getElementById('mycheckbox').onchange = myFunction(this);

function myFunction(chckbx){
console.log(chckbx);
}

see below snippet 见下面的片段

 function checkOnlyOne(slno, obj) { //Error here console.log(obj); //$('.serviceable_check_1').not(this).prop('checked', false); if(obj.checked){ $('.serviceable_check_1').not(obj).prop('checked', false); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label class="checkbox"><input type="checkbox" class="serviceable_check_1" id="serviceable1" name="condition_of_materials[]" onclick="checkOnlyOne(1,this); return false;" ><span> Serviceable</span></label> 

you can get the event object 你可以得到事件对象

Serviceable 可维修的

function checkOnlyOne(slno, e {
    console.log(e.target);
    //$('.serviceable_check_1').not(e.target).prop('checked', false);
    if(this.checked)
        $('.serviceable_check_1').not(e.target).prop('checked', false);
}

e.target is the element you clicked e.target是您单击的元素

您应该使用.call并将obj作为第一个参数传递,以将其用作this上下文

<input id="serviceable1" onclick="checkOnlyOne.call(obj, 1); return false;" ><span> Serviceable</span></label>

First for the error: you cannot name an argument "this" since it is a reserved word. 首先要指出的错误:您不能命名参数“ this”,因为它是保留字。

Now, "this" refers to its context of execution, thus, the onclick handler is a method of the element, and therefore will be excecuted in it's context (input element in our case). 现在,“ this”是指其执行上下文,因此,onclick处理程序是该元素的方法,因此将在其上下文(在本例中为input元素)中执行。

Notice the second alert in the snippet, you will see that onclick calls the function you defined from its body, which means that this function we've defined is not a method of the input object element and therefore not "attached" to it and will be called from the context in which it was declared(the window object). 注意片段中的第二个警报,您将看到onclick从其主体中调用了您定义的函数,这意味着我们定义的该函数不是输入对象元素的方法,因此不会“附加”到该对象,并且将从声明它的上下文(窗口对象)中调用。

 var value = "I'm the value out of the input object"; function displayMessage(context){ //we save the original context as arg alert("input's value: " + context.value); alert("the onclick handler:" + context.onclick); //but notic that the context has changed alert("the context in our function: " + this); alert("when trying without saving the previous context: " + this.value); } 
 <input type="checkbox" id="subscribeToNews" value="Newsletter" onclick="displayMessage(this)"/> <label for="subscribeToNews" >Subscribe </label> 

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

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