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

时间:2018-06-03 00:36:10

标签: javascript php jquery templates smarty

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

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");
}
});

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

1 个答案:

答案 0 :(得分:1)

喜欢这个吗?

&#13;
&#13;
    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');
    }
&#13;
&#13;
&#13;