dsaff82024
2018-06-03 00:36
浏览 41

Smarty输入字段中的动态主机名,点击事件为jQuery

At the moment I am trying to create an dynamic hostname with an jQuery on click event. I have this so far

Smarty:

<div>
  <input class="hostname-box" type="text" name="domain" required="" value="">
  <a href="#host" class="button">Add</a>
</div>

<div>
  <a href="#submit" class="button">Order</a>
</div>

jQuery:

$('.button').click(function(){
var jHostName = $('.hostname-box');
var hostNameValue = jHostName.text().trim();

var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

for( var i=0; i < 5; i++ )
   text += possible.charAt(Math.floor(Math.random() * possible.length));

if(hostNameValue === '') {
     jHostName.val(text+".hostname.local");
}
});

I can generate an text on click like random.hostname.local. But the goal is to achieve to generate an text like vps{random}-{currentDate}.hostname.local.

图片转代码服务由CSDN问答提供 功能建议

目前我正在尝试使用jQuery on click事件创建动态主机名。 我到目前为止

Smarty:

 &lt; div&gt; 
&lt; input class =  “hostname-box”type =“text”name =“domain”required =“”value =“”&gt; 
&lt; a href =“#host”class =“button”&gt;添加&lt; / a&gt; 
&lt;  ; / div&gt; 
 
&lt; div&gt; 
&lt; a href =“#submit”class =“button”&gt;订单&lt; / a&gt; 
&lt; / div&gt; 
    
 
 

jQuery:

  $('。button')。click(function(){
var jHostName = $(  '.hostname-box'); 
var hostNameValue = jHostName.text()。trim(); 
 
var text =“”; 
var possible =“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789”; 
 
for(var i = 0; i  &lt; 5; i ++)
 text + = possible.charAt(Math.floor(Math.random()* possible.length)); 
 
if(hostNameValue ===''){
 jHostName.val(  text +“。hostname.local”); 
} 
}); 
   
 
 

我可以像click.hostname.local一样生成点击文本。 但目标是实现生成 vps {random} - {currentDate} .hostname.local 等文本。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzouhe9734 2018-06-03 00:56
    已采纳

    Like this?

        var hostNameValue = '';
        var text = "vps-";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    
        for( var i=0; i < 5; i++ ) {text += possible.charAt(Math.floor(Math.random() * possible.length));}
    
        let date = new Date();
        text += '-'+date.toISOString().substr(0,10);
      
        if(hostNameValue === '') {
          console.log(text+'.hostname.local');
        }

    </div>
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题