简体   繁体   English

html表单重置没有触发select onchange

[英]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. 但是当我点击resetselect重置为默认值,但是不会触发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');
});`

you can use 您可以使用

$('select[name="roundType"]').prop('selectedIndex',0);

DEMO HERE 在这里演示

This may help you, replace alert lines with your activity code. 这可以帮助您,用您的活动代码替换警报线。

JSFiddle 的jsfiddle

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM