admin管理员组文章数量:1430077
I have a select control nested inside a table cell. The Table row that contains the table cell has an onclick event. When I click on the select control to change the value the onclick event of the row is fired.
I've tried using the stoppropagation method on the onchange event but it doesn't seem to work.
Below is my code
xxxxxxx.Helper.DDGoto = function (o, e) {
e.stopPropagation();
var path = $(o).val();
if (path != null) {
this.Goto(path);
}
}
<tr onclick="somemethod()">
<td>some text</td>
<td>
<select onchange="xxxxxxx.Helper.DDGoto(this, event)">some options</select>
</td>
</tr>
I have a select control nested inside a table cell. The Table row that contains the table cell has an onclick event. When I click on the select control to change the value the onclick event of the row is fired.
I've tried using the stoppropagation method on the onchange event but it doesn't seem to work.
Below is my code
xxxxxxx.Helper.DDGoto = function (o, e) {
e.stopPropagation();
var path = $(o).val();
if (path != null) {
this.Goto(path);
}
}
<tr onclick="somemethod()">
<td>some text</td>
<td>
<select onchange="xxxxxxx.Helper.DDGoto(this, event)">some options</select>
</td>
</tr>
Share
Improve this question
edited Oct 13, 2019 at 10:59
halfer
20.4k19 gold badges109 silver badges202 bronze badges
asked Aug 1, 2016 at 10:20
Richard BanksRichard Banks
2,9735 gold badges40 silver badges73 bronze badges
3
-
Your
xxxxxxx.Helper.DDGoto
is not fired byclick
, but bychange
. So thee.stopPropagation();
inside is never executed! – cFreed Commented Aug 1, 2016 at 10:26 -
Add the
$('select').on('click', function(e) { e.stopPropagation();})
and it works – Marcos Pérez Gude Commented Aug 1, 2016 at 10:30 - Is this because onchange and click are not the same event type? – Richard Banks Commented Aug 1, 2016 at 10:42
2 Answers
Reset to default 2The event triggered with the onchange
event on your select
is not firing the onclick
event on your tr
, they are not of the same type. They are two different events. That's also why stopping propagation of the onchange
event does not stop the onclick
event to be triggered.
You should instead in your onclick
handler check if the select
element was clicked or not:
somemethod = function (event) {
if (event.target.nodeName === 'SELECT') {
// The select was clicked, stopping...
return;
}
// The select was not clicked, go ahead...
}
To get the event in your handler, you need to change your declaration to:
<tr onclick="somemethod(event)">
I used the nodeName
property for the example, but you could also use the type
property of the target or any other method to detect if the select was clicked or not.
You can check this fiddle for an example.
When you click on select tag, both onChange and onClick event is fired! I solved this by creating a onClick handler along side onChange handle and then calling e.stopPropogation() function.
Worked for me!
<select (click)="StopPropogation($event)" (change)="textureChanged($event,i)" class="form-control" id="sel1">
<option selected="isTextureSolid(nodule.texture)">solid</option>
<option selected="isTexturePartSolid(nodule.texture)">part-solid</option>
<option selected="isTextureNonSolid(nodule.texture)">non-solid</option>
</select>
Javascript code:
StopPropogation(e: Event) {
e.stopPropagation();
console.log('Click event stopped from propogating');
}
textureChanged(e: Event, i: number) {
e.stopPropagation();
console.log('textTure Changed');
let btnId = 'save' + i.toString();
document.getElementById(btnId).style.display = 'block';
document.getElementById(btnId).style.visibility = 'visible';
}
本文标签: javascriptCancelling propagation of the onchange event of a select controlStack Overflow
版权声明:本文标题:javascript - Cancelling propagation of the onchange event of a select control - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745526163a2661844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论