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

javascript - In Dropdown list onchange event - Stack Overflow

programmeradmin0浏览0评论

My code is as follows:

function tab() {
  current = document.getElementById("test");
  next = document.getElementById("run");
  if (current.value != "-1") {
    next.focus()
  }
}
<select id="test" onchange="tab()">
  <option value="-1">--select--</option>
  <option value="1">TEST</option>
  <option value="2">RUN</option>
</select>
<input type="text" name="run"/>

My code is as follows:

function tab() {
  current = document.getElementById("test");
  next = document.getElementById("run");
  if (current.value != "-1") {
    next.focus()
  }
}
<select id="test" onchange="tab()">
  <option value="-1">--select--</option>
  <option value="1">TEST</option>
  <option value="2">RUN</option>
</select>
<input type="text" name="run"/>

Definition: I have a dropdown which is having four values. If I change one value to another value in the dropdown it autotab to the textbox. As per my code, it is working fine.

But the issue is when I select 1st value in the dropdown then the autotab is working and again I select the same value from the dropdown (autotab is not working). I know that the problem is in the event. There is no change so the event won't fire. Please help me to rectify the issue.

Share Improve this question edited Jan 7 at 8:54 Naeem Akhtar 1,2701 gold badge12 silver badges23 bronze badges asked Mar 8, 2013 at 11:41 user2143039user2143039 712 silver badges5 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 0

There is no element with id run, which is being referenced by:

document.getElementById("run");

You probably intended to write:

<input type="text" name="run" id="run"/>

Use onBlur() instead of onchange().

The onBlur event is fired when you have moved away from an object without necessarily having changed its value.

The onChange event is only called when you have changed the value of the field.

please have a lookinto this to know about events

Try using mouseup & keyup event as a work around:

var current = document.getElementById("test");
var next = document.getElementById("run");
var open = false; //drop-down closed

var watchOpen = function() {
  open = !open; //inverse flag to identify state of drop-down
};

var tab = function() {
  if (!open && current.value !== "-1") {
    next.focus();
  }
};
<select id="test" onmouseup="watchOpen();tab();" onkeyup="watchOpen();tab();">
  <option value="-1">--select--</option>
  <option value="1">TEST</option>
  <option value="2">RUN</option>
</select>
<input type="text" id="run" /><!-- replaced name with id -->

try to use onBlur() .It will solve the problem

I think this is possible with html5

onselect

Try this :

<!DOCTYPE html>
<html>
  <body>
    <select id="test" onchange="tab()">
      <option value="-1">--select--</option>
      <option value="1">TEST</option>
      <option value="2">RUN</option>
    </select>
    <input type="text" name="run" id="run" />
    <script type="text/javascript">
      function tab() {
        current = document.getElementById("test");
        next = document.getElementById("run");
        if (current.value != "-1") {
          next.focus();
          next.value = current.options[current.selectedIndex].text;
        } else {
          next.value = "";
        }
      }
    </script>
  </body>
</html>

Just make a small change in your code and you are good to go.

<select id="test" onchange="tab()" onblur="tab()">
        <option value="-1">--select--</option>
        <option value="1">TEST</option>
        <option value="2">RUN</option>
</select>

This will work.

To make the auto-tab work even if the same value is selected again, you can use the on click event in bination with onchange. This way, even if the user selects the same option again, the focus will shift.

function tab() {
  current = document.getElementById("test");
  next = document.getElementById("run");
  if (current.value != "-1") {
    next.focus();
  }
}

function resetDropdown() {
  document.getElementById("test").value = "-1";
}
<select id="test" onchange="tab()" onclick="resetDropdown()">
  <option value="-1">--select--</option>
  <option value="1">TEST</option>
  <option value="2">RUN</option>
</select>

<input type="text" id="run" name="run"/>

发布评论

评论列表(0)

  1. 暂无评论