English 中文(简体)
jquery - 动态生成
原标题:jquery - Dynamically generate url

我正试图在用户类别输入投入时,动态地制造一种ur。 应当消除不受欢迎的特征。 空间应当由hy子和所有东西方取代。 因此,如果用户类型“Ruddy s Cheese Hotel”,就应当“ruddys-cheese-shop”。

这是我迄今为止所做的。

<input id="tb1" />
<div id="tb2"></div>

$( #tb1 ).keyup(function() {
  var Text = $( #tb1 ).val();
  Text = Text.toLowerCase();
  Text = Text.replace(/[^a-zA-Z0-9]+/g, - );
  $( #tb2 ).html($( #tb1 ).val(Text));
});

It almost works but I am new to js. Thanks

最佳回答

你的法典,但略有改进。

$( #tb1 ).keyup(function() {
    var text = $(this).val().toLowerCase();
    text = text.replace(/[^a-z0-9]+/g,  - );
    $( #tb2 ).text(text);
});

You don t need to find $( #tb1 ) element over and over again since you have a refference to it inside the function as $(this).

http://jsfiddle.net/jFjR3/

问题回答

只看一.,除非你确定点2值。 我想你想要:

$( #tb2 ).html(Text);

当然,由于你们叫到LowerCase,你不需要取代上个案例。 更简单地说:

Text = Text.replace(/[^a-z0-9]+/g, - );

如果你也希望更新用户类型,就是一个充分的例子。 请注意,只有在你开始打字时才会更新编号2。

  <html>
    <head>
      <script src="js/jquery.js" ></script>
      <script language="javascript">
      $(function() {
        $( #tb1 ).keyup(function() {
           var Text = $( #tb1 ).val();
           Text = Text.toLowerCase();
           Text = Text.replace(/[^a-z0-9]+/g, - );
           $( #tb2 ).html(Text);
           $( #tb1 ).val(Text);
        });
      });
      </script>
    </head>
  <body>
  <input type="text" id="tb1" value="Ruddy s Cheese Shop" />
  <div id="tb2"></div>
  </body>
  </html>

看像你那样需要双管,即。

Text = Text.replace(/[s]+/g, - ); 
Text = Text.replace(/[^a-z_0-9-]+/g,  );

这把精炼的切塞店改为rud店

帮助的希望





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签