[英]html form reset not doing triggering select onchange
I am having a form
where the fields need to change according to my select
. 我有一个
form
,其中字段需要根据我的select
进行更改。 But when I hit the reset
the select
resets back to default, but the onchange
event on the select
is not triggered. 但是当我点击
reset
, select
重置为默认值,但是不会触发select
上的onchange
事件。 Is there anyway so that I can add that to my javascript? 反正有没有这样我可以添加到我的JavaScript?
I am resetting using a button with type="reset"
我正在使用
type="reset"
的按钮type="reset"
$('#newHistoryPart select[name="roundType"]').on('change', function (data)
{
$(".answerType").hide();
selected = $(this).find("option:selected").val();
roundTypeChange(selected);
});
From my comment above, use onreset
event instead of onchange
: 从上面的评论中,使用
onreset
事件而不是onchange
:
$('#yourform').on('reset', function(){
// do something
});
What you need to do is, trigger the change event manually when the reset button is clicked. 您需要做的是,单击重置按钮时手动触发更改事件。 See Fiddle here
在这里看小提琴
$('select').on('change', function ()
{
alert('on change');
});
$('input[type="reset"]').click(function() {
$("select").trigger('change');
});`
This may help you, replace alert lines with your activity code. 这可以帮助您,用您的活动代码替换警报线。
HTML HTML
<select name="opt" onchange="getval(this)">
<option value="Select" selected disabled>Select</option>
<option value="op1">Option 1</option>
<option value="op2">Option 2</option>
</select>
JavaScript JavaScript的
function getval(sel) {
if (sel.value == "op1") {
alert("Option 1 Selected");
} else if (sel.value == "op2") {
alert("Option 2 Selected");
}
else
{
alert("EXCEPTION !");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.