最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - html form reset not doing triggering select onchange - Stack Overflow

programmeradmin1浏览0评论

I am having a form where the fields need to change according to my select. But when I hit the reset the select resets back to default, but the onchange event on the select is not triggered. Is there anyway so that I can add that to my javascript?

I am resetting using a button with type="reset"

    $('#newHistoryPart select[name="roundType"]').on('change', function (data) 
    {
      $(".answerType").hide();
      selected = $(this).find("option:selected").val();
      roundTypeChange(selected); 
    });

I am having a form where the fields need to change according to my select. But when I hit the reset the select resets back to default, but the onchange event on the select is not triggered. Is there anyway so that I can add that to my javascript?

I am resetting using a button with type="reset"

    $('#newHistoryPart select[name="roundType"]').on('change', function (data) 
    {
      $(".answerType").hide();
      selected = $(this).find("option:selected").val();
      roundTypeChange(selected); 
    });
Share Improve this question edited May 13, 2015 at 12:32 Deep Kakkar 6,3054 gold badges43 silver badges80 bronze badges asked May 13, 2015 at 12:19 MathiasMathias 2071 gold badge8 silver badges19 bronze badges 4
  • 1 Show the reset functionality. How you are doing reset? – Guruprasad J Rao Commented May 13, 2015 at 12:20
  • 2 You can use onreset event, which is different that onchange. This is the cause. – kosmos Commented May 13, 2015 at 12:21
  • 1 @GuruprasadRao he clearly states he's using a button with type reset. – Joe Swindell Commented May 13, 2015 at 12:22
  • @JoeSwindell Ok!! My bad!! Missed that part !! – Guruprasad J Rao Commented May 13, 2015 at 12:23
Add a comment  | 

4 Answers 4

Reset to default 8

From my comment above, use onreset event instead of 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

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

function getval(sel) {
    if (sel.value == "op1") {
        alert("Option 1 Selected");
    } else if (sel.value == "op2") {
        alert("Option 2 Selected");
    }
    else
    {
        alert("EXCEPTION !");
    }
}
发布评论

评论列表(0)

  1. 暂无评论