[英]Triggering click on button when select option changes doesn't work properly
I have the following code to trigger a click on a button whenever the state changes: 每当状态更改时,我就有以下代码来触发对按钮的单击:
$('#selectstate').on('change', function(){
$("[name='update']:submit").trigger("click");
});
It works fine, but the problem is that I have to click exactly on the arrow of the list, as appears below. 它工作正常,但问题是我必须完全单击列表中的箭头,如下所示。 If I clicked on the white space in order to show the list, the button will be triggered automatically.
如果单击空白以显示列表,该按钮将自动触发。 Any solution to this?
有什么解决办法吗?
Sample HTML Code: 示例HTML代码:
<select id="selectstate" name="state">
<option value="Alabama">Alabama</option>
<option value="Kansas">Kansas</option>
<option value="Kentucky">Kentucky</option>
</select>
<input type="submit" value="Update" name="update" />
I think you can simply submit your form. 我认为您只需提交表格即可。
var yourForm = document.forms["update"]; // your form name is "update"
yourForm.submit();
If you really want to click while changing the select box then create a click
event and trigger it. 如果您真的想在更改选择框时单击,则创建一个
click
事件并触发它。
var selectBox = document.getElementById("yourSelect");
selectBox.onchange = function()
{
// call the fireClick function passing the element
fireClick(link); // pass the element in which you want to trigger
}
function fireClick(element)
{
var ev = document.createEvent("MouseEvents");
ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
element.dispatchEvent(ev);
}
even more simple way 更简单的方法
selectBox.onchange = function()
{
var submitButton = document.getElementById("button");
submitButton.click();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.