【问题标题】:Have a select menu select a value in another select menu with Jquery Mobile [duplicate]让一个选择菜单使用 Jquery Mobile 在另一个选择菜单中选择一个值 [重复]
【发布时间】:2017-06-14 21:15:09
【问题描述】:

我试图让优先级下拉菜单在用户选择选项时动态更改另一个下拉菜单的值。我正在使用 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>

【问题讨论】:

    标签: jquery jquery-mobile


    【解决方案1】:

    这是一个例子:

    $(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属性

    【讨论】:

    • 谢谢,效果很好。我将如何为此添加多个优先级实例。 (即如果我选择了 Severe,它应该在 commETA 和 compETA 中选择今天?)再次感谢!
    • cases 对象已经计划以所有不同的优先级进行扩展,但我不确定你的“instances”是什么意思...
    • 您的示例实际上向我展示了您是如何做到的。我在 } 之后错过了 , 。再次感谢 deblocker!
    • $(elm).val("value"); $(elm).selectmenu("refresh"); 动态选择的正确方法。
    • 使用.val()。它应该可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    相关资源
    最近更新 更多