2016-08-01 5 views
-2

ダイアログウィンドウでjquery関数を使用してみます。関数はクラス ".numeric"に割り当てられます。 htmlの入力にクラスを追加するとうまくいきますが、ダイアログでは機能しません。ダイアログボックスがさえ作成される前に、あなたの$(".numeric").numeric();コードは、一度だけ呼び出されるためですjquery-uiダイアログのjquery関数

// numeric function 
 
!function($){$.fn.alphanumeric=function(p){var input=$(this),az="abcdefghijklmnopqrstuvwxyzążśźćłóę",options=$.extend({ichars:"[email protected]#$%^&*()+=[]\\';,/{}|\":<>?~`.- _",nchars:"",allow:"",limit:0,counter:""},p),s=options.allow.split(""),i=0,ch,regex;for(i;i<s.length;i++)-1!=options.ichars.indexOf(s[i])&&(s[i]="\\"+s[i]);if(options.nocaps&&(options.nchars+=az.toUpperCase()),options.allcaps&&(options.nchars+=az),options.limit&&options.limit>0){var interval,f,self=input;$(this).focus(function(){interval=window.setInterval(substring,100)}),$(this).bind("keyup keypress blur change",function(){clearInterval(interval),substring()}),substringFunction="function substring(){ var val = $(self).val();var length = val.length;if(length > options.limit){$(self).val($(self).val().substring(0,options.limit));}","undefined"!=typeof $(options.counter)&&(substringFunction+="if($(options.counter).html() != options.limit - length){$(options.counter).html((options.limit - length<=0)?'0':options.limit - length);}"),substringFunction+="}",eval(substringFunction),substring()}return options.allow=s.join("|"),regex=new RegExp(options.allow,"gi"),ch=(options.ichars+options.nchars).replace(regex,""),input.keypress(function(n){var i=String.fromCharCode(n.charCode?n.charCode:n.which);-1==ch.indexOf(i)||n.ctrlKey||n.preventDefault()}),input.blur(function(){var n=input.val(),i=0;for(i;i<n.length;i++)if(-1!=ch.indexOf(n[i]))return input.val(""),!1;return!1}),input},$.fn.numeric=function(n){var i="abcdefghijklmnopqrstuvwxyzążśźćłóę",t=i.toUpperCase();return this.each(function(){$(this).alphanumeric($.extend({nchars:i+t},n))})},$.fn.alpha=function(n){var i="1234567890";return this.each(function(){$(this).alphanumeric($.extend({nchars:i},n))})}}(jQuery); 
 

 
$(document).ready(function() { 
 

 
    // if numeric function is present then define some classes 
 
    if (typeof $.fn.numeric == "function") { 
 
    $(".numeric").numeric(); 
 
    } 
 

 
    $(".openWindow").click(function() { 
 

 
    var form = ` 
 
     <form method="post"> 
 
      <label for="onlynumbers">Input with numbers:</label> 
 
      <input type="text" id="onlynumbers" name="onlynumbers" value="" class="numeric" required /> 
 
     </form> 
 
    `; 
 

 
    var $dialog = $("<div></div>").html(form).dialog({ 
 
     height: 100, 
 
     width: 400, 
 
     title: 'Here is a problem' 
 
    }); 
 
    $dialog.dialog("open"); 
 

 
    }); 
 

 
});
#layout { 
 
    padding: 25px; 
 
} 
 
#layout form { 
 
    margin-bottom: 25px; 
 
}
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.11.4/themes/redmond/jquery-ui.css" media="screen" /> 
 
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.0.min.js"></script> 
 
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.11.4/jquery-ui.min.js"></script> 
 
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.11.4/i18n/jquery-ui-i18n.min.js"></script> 
 

 
<div id="layout"> 
 
    <p>Look: this input accept only numbers.</p> 
 
    <form method="post"> 
 
    <label for="onlynumbers">Input with numbers:</label> 
 
    <input type="text" id="onlynumbers" name="onlynumbers" value="" class="numeric" required /> 
 
    </form> 
 

 
    <button type="button" class="openWindow">Open dialog window</button> 
 

 
</div>

答えて

0

ので、このコードは動作するはずです:

 $(".openWindow").click(function(){ 

      var form = ` 
       <form method="post"> 
        <label for="onlynumbers">Input with numbers:</label> 
        <input type="text" id="onlynumbers" name="onlynumbers" value="" class="numeric" required /> 
       </form> 
      `; 

      var $dialog = $("<div></div>").html(form).dialog({ height: 100, width: 400, title: 'Here is a problem'}); 
      $(".numeric").numeric(); 
      $dialog.dialog("open"); 

     }); 
+0

をはい、もちろんそれが動作します。しかし、それを「もっとグローバルに」定義する機会はありますか? 「数値」クラスは、ダイアログウィンドウで使用したい唯一のクラスではありません。 – Miramar

+0

あなたは '$ dialog.find(" input [type = text] ")などの何かをすることができます。数値();' –

+0

私は他の方法を見つけなければなりません。いくつかのフィールドは数字だけであり、いくつかはテキストなどです。私が持っているすべての関数を使う必要があります。私はそれらを二重にしません。しかし、助けと関心のおかげで。 – Miramar

関連する問題