jQuery Mobile根据选择字段显示/隐藏表单输入

jQuery Mobile Show/Hide Form input based on select field

本文关键字:隐藏 表单 输入 显示 字段 Mobile 选择 jQuery      更新时间:2023-09-26

jQuery Mobile页面上有以下表单字段。我想一开始隐藏元素,然后如果用户选择值为"Other"的选项,就显示它

这是我的HTML:

<div data-role="fieldcontain" class="no-field-separator">
        <select name="plan_height" id="plan_height" data-native-menu="false">
          <option>Plan Height</option>
          <option value="Standard 6foot 2inch">Standard 6'2"</option>
          <option value="Other">Specify in Notes</option>
        </select>
      </div>
      <div id="specify_plan_height_box" style="display:none;">
        <div data-role="fieldcontain" class="ui-hide-label no-field-separator">
          <label for="specify_plan_height">Specify Plan Height</label>
          <input type="text" id="specify_plan_height" name="specify_plan_height" placeholder="Specify Plan Height" maxlength="50" />
        </div>
      </div>

这是我在页面内的JS:

 // $( '#machine-guarding-page' ).live( 'pageinit',function(event) {
$( document ).bind( "pageinit", function( event, data ) {
    $('#plan_height').change(function() {
        var planHeightVal = $("#plan_height option:selected").val();
        var sphb          = $("#specify_plan_height_box");
        sphb.hide();
        if (planHeightVal == "Other") {
            sphb.show();
        }
    });
});
    });

在这个工作示例中,它工作得很好。我需要$(".page").live('pageinit', function() {以外的东西吗在我的JS代码顶部,让它在页面加载后工作?它在上面的示例链接中工作得很好,但在我的jQueryMobile网站上不起作用。

这对你有用吗:

  • http://jsfiddle.net/Qe6G4/1/
  • http://jsfiddle.net/Qe6G4/2/(已优化)
  • http://jsfiddle.net/Qe6G4/3/(带有另一个输入)