2012-12-14 24 views
0

我有几个弹出窗口有多个输入字段。我想强调我的所有弹出窗口的FIRSTinput元素,我不想使用IDS:使用jquery突出显示弹出框的第一个输入元素

我的代码:

function initPopups(){ 
    $('*[id*=Popup]').each(function() { 
     $(this).on({ 
        // disables the auto close on outer clicks 
      popupbeforeposition: function() { 
       // removes all handlers from the 'background'(-layer.class) of the popup 
       $('.ui-popup-screen').off(); 
      }, 
        // SHOULD highlight the first textfield after popup opened 
      popupafteropen:function(){ 
       console.log("OPENED"); 
        $('form:first *:input[type!=hidden]:first').focus(); 
      } 
     }); 
    }); 

这里有一个弹出式的HTML:

<div data-role="popup" id="create_stationPopup" class="ui-content"> 
      <a href="#" data-rel="back" data-role="button" data-theme="c" data-icon="delete" data-iconpos="notext" 
       class="ui-btn-right">Close</a> 
      <form> 
      <input type="text" name="name" id="station_input_title" value="" data-mini="true" placeholder="Titel der Station" /> 
      <input type="text" name="name" id="station_input_number" value="" data-mini="true" placeholder="Nummer der Station"/> 
      <textarea name="textarea" id="station_input_text" placeholder="Beschreibe diese Station"></textarea> 
      </form> 
      <a href="#create_stationQuestionPopup" data-rel="popup" data-role="button" data-theme="c" id="createWave_questionBtn" >Frage 
       hinzufügen</a> 
      <a id="createWave_stationSaveBtn" data-role="button" data-theme="c">Station 
       speichern</a> 
     </div> 

我的代码只突出显示我的HTML文件中的第一个输入字段,而不是所有弹出窗口中的第一个输入字段(它们都是静态html)

+0

你正在使用什么插件,从问题其不明确:( – sakhunzai

+0

这是使用jQuery手机? – David

+0

jquery mobile yes。 – MJB

回答

1

与@Trufa的帮助下,我固定它通过以下方式:

function initPopups(){ 
    $('*[id*=Popup]').each(function() { 
     $(this).on({ 
      popupbeforeposition: function() { 
       // removes all handlers from the 'background'(-layer.class) of the popup 
       $('.ui-popup-screen').off(); 

      }, 
      popupafteropen:function(){ 
        $('.ui-popup-active *:input[type!=hidden]:first').focus(); 
       } 
     }); 
    }); 
} 

每弹出获取类.ui-popup-active当它是屏幕,所以只需使用它,并突出显示第一个输入字段:)

0

尝试使用[0]选择第一个元素i n中的“数组”的jQuery input元素在form

popupafteropen:function(){ 
    console.log("OPENED"); 
    $('div[data-role=popup] input[type!=hidden]')[0].focus(); 
} 
+0

可悲的是它不起作用 – MJB