使用Jquery Mobile让选择菜单在另一个选择菜单中选择一个值 [英] Have a select menu select a value in another select menu with Jquery Mobile

查看:94
本文介绍了使用Jquery Mobile让选择菜单在另一个选择菜单中选择一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to have a priority level dropdown menu change a value of another dropdown menu dynamically when the user selects an option. I am using Jquery Mobile and not sure how I would go about making this happen. Here is my HTML currently. So if I choose priority level Normal I would like it so select 48 Hours in the commETA and compETA select boxes. Thanks in advance for any help!

<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>

推荐答案

以下是示例:

$(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:

as Omar said in a comment, val() should work just fine - the correct filter is :selected:

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

EDIT2 :初始selected属性

这篇关于使用Jquery Mobile让选择菜单在另一个选择菜单中选择一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆