开发者

value of textarea : problem in firefox

开发者 https://www.devze.com 2023-01-20 04:19 出处:网络
I have written a very simple script with jQuery, but it does not work in Firefox: <a href=\"\" class=\"po\">any text</a>

I have written a very simple script with jQuery, but it does not work in Firefox:

<a href="" class="po">any text</a>
<form>
    <input type="text" class="infobox" />
    <br />
    <textarea class="me"></textarea>
    <input type="submit"   value="click"  class="submit" />
</form>

jQuery

var vl = $('.po').text();
$('.po').click(function(){
    $('.me').val(vl);
});

Why does开发者_开发百科 this script not work in Firefox? Thanks in advance


$(".po") is a link, so you should use event.preventDefault() or return false so you don't navigate away from the page when you click it.

Also make sure you are using script tags

<a href="" class="po">any text</a>
<form>
<input type="text" class="infobox"/>
<br />
<textarea class="me"></textarea>
<input type="submit"   value="click"  class="submit"/>
</form>
<script type="text/javascript">
var vl = $('.po').text();
$('.po').click(function(event){
    $('.me').val(vl);
    event.preventDefault();
});
</script>

Try it out with this jsFiddle

Or, you can put the JS in the head like this:

<html>
<head>
<script type="text/javascript">
$(function() {                                                   \\ <== doc ready
    var vl = $('.po').text();
    $('.po').click(function(event){
        $('.me').val(vl);
        event.preventDefault();
    });
});
</script>
</head>
<body>
<a href="" class="po">any text</a>
<form>
<input type="text" class="infobox"/>
<br />
<textarea class="me"></textarea>
<input type="submit"   value="click"  class="submit"/>
</form>
</body>
</html>

Try it out with this jsFiddle


Your code seems to be fine, try putting it in ready handler:

<script type="text/javascript">
$(function(){
  var vl = $('.po').text();
  $('.po').click(function(){
      $('.me').val(vl);
      return false;
  });
});
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消