Trigger()、Change()或Click()Question来针对那些具有特定值的按钮



根据我在StackOverflow上看到的情况,这些函数似乎只针对具有特定Id或class的特定元素,例如Id为first的按钮,以触发Id为second的按钮而不实际触摸第二个按钮。但我正在寻找一个做同样事情的按钮,但由Val而不是id或类,因为我有多个按钮具有相同的类,但有它们自己的值。这是我的一段代码

$('<center class='centers'><button class='command_button buttons' value='Intro2/2'>Next</button></center>').appendTo('#choicebox');

这是触发事件的按钮,它有一类按钮,但Intro2/2 的值

$(document).on('click', '.buttons', function() {
var button = $(this).val();
console.log('The Button is ' + button);
CL();
$('#container').scrollTop($('#container')[0].scrollHeight);
switch (button) {
// Go from Introduction 1/2 to Introduction 2/2.
case 'Intro2/2': {
clean();

CCEvents('Intro2/2');
} break;

正如你所看到的,有一个.on函数可以查找任何元素的点击事件,一旦找到,它将接受button的值并将其添加到按钮的变量中,然后使用该变量在开关/案例中找到一个,并将其匹配到Intro2/2,这将触发我想要的事件函数CCEvents("Intro2/2"(。

所有这些都已经完成了,但我正在尝试将其添加到我的地图系统中,但需要一些方法来触发按钮,而无需实际点击按钮,或者直接触发CCEvents("Intro2/2"(

这是我尝试使用触发器和单击,也许我不明白它是如何工作的,也不明白为什么它在使用这些功能时不起作用。

$("<button class='command_button buttons' value='Intro2/2'>Next</button>").trigger("click");
$(".buttons").val("Intro2/2").change();
$('.buttons').trigger('Intro2/2');
$(".buttons").val("Intro2/2").click();
$(".buttons").trigger("click", "Intro2/2");

您可以使用类似$('.buttons[value="Intro2/2"]').click()的属性选择器

$('<center class='centers'><button class='command_button buttons' value='Intro2/2'>Next</button></center>').appendTo('#choicebox');
$(document).on('click', '.buttons', function() {
var button = $(this).val();
console.log('The Button is ' + button);
})

$('.buttons[value="Intro2/2"]').click()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="choicebox"></div>

最新更新