选择菜单使用Jquery Mobile在另一个选择菜单中选择一个值

时间:2017-06-14 21:15:09

标签: jquery jquery-mobile

我正在尝试使用优先级下拉菜单在用户选择选项时动态更改另一个下拉菜单的值。我正在使用Jquery Mobile而不确定如何实现这一目标。这是我目前的HTML。因此,如果我选择优先级正常,我希望在commETA和compETA选择框中选择48小时。在此先感谢您的帮助!

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Login</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link rel="stylesheet" href="../css/themes/Estimates.css" />
	<link rel="stylesheet" href="../css/themes/jquery.mobile.icons.min.css" />
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />
	<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
	<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
  </head>

  <body>
    <div data-role="page" data-theme="a">
		<div data-role="header" data-position="inline">
			<h1>MGK Electric CTA</h1>
		</div>
		<div data-role="content" data-theme="a">
       		<form class="form-signin" id="form1" method="post" action="<?php echo $formaction; ?>">
        		<h4 class="form-signin-heading" style="text-align: center;">Please describe CTA Below</h4>
        		<label style="font-weight: bold;">Customer Name:</label>
        		<input name="Customer_Name" id="Customer_Name" type="text" class="form-control" autofocus required="required"><br />
        		<label style="font-weight: bold;">Priority Level:</label>	
       		  	<select name="Priority" id="Priority" required="required">
        		  	<option value="">Please Select</option>
        		  	<option value="Normal">Normal</option>
        		  	<option value="High">High</option>
        		  	<option value="Severe">Severe</option>
        		  	<option value="Emergency">Emergency</option>
        		</select><br />
        		<label style="font-weight: bold;">Project:</label>
        		<input name="Project" id="Project" type="text" class="form-control"><br />
        		<label style="font-weight: bold;">Expected Time for Communication:</label>
        		<select name="commETA" id="commETA" required="required">
        			<option value="">Please Select</option>
        		  	<option value="Today">Today</option>
        		  	<option value="48 Hours">48 Hours</option>
        		  	<option value="1 Week">1 Week</option>
        		  	<option value="2 Weeks">2 Weeks</option>
        		</select><br />
        		<label style="font-weight: bold;">Expected Time for Completion:</label>
        		<select name="compETA" id="compETA" required="required">
        			<option value="">Please Select</option>
        		  	<option value="Today">Today</option>
        		  	<option value="48 Hours">48 Hours</option>
        		  	<option value="1 Week">1 Week</option>
        		  	<option value="2 Weeks">2 Weeks</option>
        		  	<option value="1 Month">1 Month</option>
        		  	<option value="Unknown">Unknown</option>
        		</select><br />
        		<label style="font-weight: bold;">Description of Issue:</label>
        		<textarea name="issue" id="issue" required="required"></textarea><br />
        		<label style="font-weight: bold;">Unknowns About Issue:</label>
        		<textarea name="unknowns" id="unknowns" required="required"></textarea><br />
        		<label style="font-weight: bold;">People Invoved in CTA:</label>
        		<textarea name="people" id="people" required="required"></textarea>
        		<br /><br />
        		<input type="hidden" name="doform" value="doform">
        		<button name="Submit" id="submit" class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>

        		<div id="message"></div>
      		</form>

    	</div> 
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script type="text/javascript" src="login/js/bootstrap.js"></script>
  </body>
</html>

1 个答案:

答案 0 :(得分:2)

以下是一个例子:

$(document).on("pagecreate", "#page-form1", function() {
  $("select").each(function(idx) {
    $(this).change(function() {
      var value = $(this).val();
      //$(this).children("option").removeAttr("selected").find("[value='" + value + "']").attr("selected", "selected");
      if($(this).attr("id") == "Priority")
        linkOptions(value);
    });
  });
});

function linkOptions(priority) {
  var cases = {
    "Severe": {
      "commETA": "Today",
      "compETA": "Today"
    },
    "Normal": {
      "commETA": "48 Hours",
      "compETA": "48 Hours"
    }
  };

  if (cases[priority]) {
    $.each(cases[priority], function(key, value) {
      $("#" + key).val(value).trigger("change");
    });
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Login</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css">
  <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
  <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
  <div data-role="page" id="page-form1" data-theme="a">
    <div data-role="header">
      <h1>MGK Electric CTA</h1>
    </div>
    <div data-role="content" data-theme="a">
      <form class="form-signin" id="form1" method="post" action="<?php echo $formaction; ?>">
        <h4 class="form-signin-heading" style="text-align: center;">Please describe CTA Below</h4>
        <label for="Customer_Name">Customer Name:</label>
        <input name="Customer_Name" id="Customer_Name" type="text" class="form-control" autofocus required="required">
        <br>
        <label for="Priority">Priority Level:</label>
        <select name="Priority" id="Priority" required="required">
          <option value="" selected="selected">Please Select</option>
          <option value="Normal">Normal</option>
          <option value="High">High</option>
          <option value="Severe">Severe</option>
          <option value="Emergency">Emergency</option>
        </select>
        <br>
        <label for="Project">Project:</label>
        <input name="Project" id="Project" type="text" class="form-control">
        <br />
        <label for="commETA">Expected Time for Communication:</label>
        <select name="commETA" id="commETA" required="required">
          <option value="" selected="selected">Please Select</option>
          <option value="Today">Today</option>
          <option value="48 Hours">48 Hours</option>
          <option value="1 Week">1 Week</option>
          <option value="2 Weeks">2 Weeks</option>
        </select>
        <br>
        <label for="compETA">Expected Time for Completion:</label>
        <select name="compETA" id="compETA" required="required">
          <option value="" selected="selected">Please Select</option>
          <option value="Today">Today</option>
          <option value="48 Hours">48 Hours</option>
          <option value="1 Week">1 Week</option>
          <option value="2 Weeks">2 Weeks</option>
          <option value="1 Month">1 Month</option>
          <option value="Unknown">Unknown</option>
        </select>
        <br>
        <label for="issue">Description of Issue:</label>
        <textarea name="issue" id="issue" required="required"></textarea>
        <br>
        <label for="unknowns">Unknowns About Issue:</label>
        <textarea name="unknowns" id="unknowns" required="required"></textarea>
        <br>
        <label for="people">People Invoved in CTA:</label>
        <textarea name="people" id="people" required="required"></textarea>
        <br>
        <br>
        <input type="hidden" name="doform" value="doform">
        <button name="Submit" id="submit" class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
        <div id="message"></div>
      </form>
    </div>
  </div>
</body>
</html>

编辑:正如Omar在评论中所说,val()应该可以正常工作 - 正确的过滤器是:selected

$("select").each(function(idx) {
  var selectedValue = $(this).find("option").filter(":selected").attr("value");
});

EDIT2:初始selected属性