简体   繁体   English

更改2个选择框之一时,用每个选择框的值进行提醒

[英]When One of 2 select boxes are changed, alert with the value for each select box

Given the following: 给定以下内容:

<div class="filters" id="filters">
<select id="state" name="state" tabindex="1" >
<option value="">Filter by State</option>
<option value="AL" >Alabama</option>
<option value="AK" >Alaska</option>
<option value="AZ" >Arizona</option>
etc...
</select>

<select id="availability" availability="products" tabindex="2">
<option value="">Filter by Availability</option>
<option value="yes">Available 24/7</option><option value="no">Not Available 24/7</option>
</select>
</div>

What kind of JQUERY magic would bind to filters, so that any time the SELECT input(s) were changed, it would alert with the Values of STATE and AVAILABILITY? 哪种JQUERY魔术会绑定到过滤器,以便在任何时候改变SELECT输入的状态时,都会发出STATE和AVAILABILITY值的警报?

Try: 尝试:

$('#filters > select').change (function () {
    alert ($('#state').val ());
    alert ($('#availability').val ());
});

Use a selector that will choose either and apply a change event handler. 使用选择器将选择其中一个并应用更改事件处理程序。 In the event handler get the value of each using a selector for each by name. 在事件处理程序中,使用每个名称的选择器获取每个值。

$('#state,#availability').change( function() {
    alert( $('#availability').val(); );
    alert( $('#state').val() );
});

You could, of course, get the values and concatenate them to output them in a single alert if needed. 当然,如果需要,您可以获取值并将其连接起来以在单个警报中输出它们。

You can add a class to both selects eg class="twoselects" and apply the change event handler to them both: 您可以将一个类添加到两个选择中,例如class =“ twoselects”并将更改事件处理程序应用于这两个选择:

  $(".twoselects").change(function(){
      state = $("#state").val();
      avail = $("#availablility").val();
      //now do something
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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