使用JavaScript或JQuery模拟用户点击 [英] Simualating user clicks using JavaScript or JQuery
问题描述
我可以使用以下脚本选择菜单项目。
代码:
函数setSelectedValue(selectObj,valueToSet){
for(var i = 0; i< selectObj.options.length; i ++){
if(selectObj.options [i] .value == valueToSet){
selectObj.options [i ] .selected = true;
return;
}
}
}
var number = document.getElementById('level1-option');
setSelectedValue(number,p3);
但是,当我这样做时,div2从未启用。我尝试了jQuery代码来在下拉菜单上发出更改信号,但它不起作用。当我调试下面的html代码时,我在那里看到了按钮标记,我立即认为它在点击时提交。但是,我没有看到任何形式。如果我使用chrome调试网站,当我在菜单中选择一个项目时,我看到代码跳转了不同的js文件。任何人都可以指导我如何找出当某个物品被选中时触发哪个信号?显然,他们在网站上做了一些窍门,以防止点击正常的方式。
代码: $ b
< div data-custom-select =class =custom-select focus>
< label for =level1-optionclass =has-hidden-label label-text> Sections< / label>
< span class =btn-select icon-down_thin>选取部分< / span>
< option value =>选择一节< / option>
< option value =p1data-href =/ callback / discovery / p1 /> P1< / option>
< option value =p2data-href =/ callback / discovery / p2 /> P2< / option>
< option value =p3data-href =/ callback / discovery / p3 /> P3< / option>
< option value =p4data-href =/ callback / discovery / p4 /> P4< / option>
< / select>
< span class =icon-down_thin>< / span>
通常您可以使用:
$(#level1-option).val(valueToSet).trigger(click)
或
$(#level1-option ).val(valueToSet).trigger(change)
但它可能取决于其余的代码在网页上。
There is a website that I want to simulate user clicks on. In this website, there is the following div, let's call it div1, where there is a dropdown menu. There are actually other two similar divs (lets call them div2 and div3) following but I didn't place here for the simplicity. When you select one of the item in the dropdown menu in div1, the div2 is enabled (disabled by default in the beginning) and its menu content is fetched from database based on the item selected from the div1.
I can select the menu item using following script.
Code:
function setSelectedValue(selectObj, valueToSet) {
for (var i = 0; i < selectObj.options.length; i++) {
if (selectObj.options[i].value== valueToSet) {
selectObj.options[i].selected = true;
return;
}
}
}
var number = document.getElementById('level1-option');
setSelectedValue(number, "p3");
However, when I do this, the div2 is never enabled. I tried jQuery code to emit change signal on the dropdown menu but it doesn't work. When I was debugging the below html code, I saw the button tag there and I immediately thought that it submits when there is click. However, I don't see any form. If I debug the website using chrome, I see that the code jumps different js files when I select an item in the menu. Could anyone guide me how I can find out which signal is triggered when an item is selected? Apparently they do some tricks in the website to prevent normal ways of clicking
Code:
<div data-custom-select="" class="custom-select focus">
<label for="level1-option" class="has-hidden-label label-text">Sections</label>
<span class="btn-select icon-down_thin">Choose a section</span>
<select class="categories-options" data-level="1" name="level1-option" id="level1-option" required="">
<option value="">Choose a section</option>
<option value="p1" data-href="/callback/discovery/p1/">P1</option>
<option value="p2" data-href="/callback/discovery/p2/">P2</option>
<option value="p3" data-href="/callback/discovery/p3/">P3</option>
<option value="p4" data-href="/callback/discovery/p4/">P4</option>
</select>
<span class="icon-down_thin"></span>
<button type="submit" class="category-submit ui-button-secondary ">Choose</button>
Usually you could use:
$("#level1-option").val(valueToSet).trigger("click")
or
$("#level1-option").val(valueToSet).trigger("change")
but it might depend on the rest of the code on the webpage.
这篇关于使用JavaScript或JQuery模拟用户点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!