开发者

Modify `input` value

开发者 https://www.devze.com 2023-04-07 06:44 出处:网络
HTML: <div class=\"mydiv\"> My Div <ul> <li><input type=\"submit\" value=\"first\"/></li>

HTML:

<div class="mydiv">
    My Div
    <ul>
       <li><input type="submit" value="first"/></li>
       <li><input type="submit" value="second (blah blah)"/></li>
       <li><input type="submit" value="third . bl开发者_如何学运维ah blah"/></li>
    </ul>
</div>

How can I retrieve the value of the input and replace any '(' or '.' with a '\n'? The result would appear like:

second
(blah blah)


Wrote this assuming you wanted to keep the separator as your exemple implied, you can see it working here.

$(document).ready(function(){
  $('.mydiv input[type=submit]').each(function() {
    $this = $(this);
    $this.val($this.val().replace(/(\.|\()/, '\n$1'));
  });
});

EDIT: @vzwick: regarding your edit no, it shouldn't be a one-liner, I'm caching $this on purpose.


That should do the trick:

$(document).ready(function(){
    $('.mydiv input').each(function(i,e){
        $(e).val($(e).val().replace('(', '\n').replace('.', '\n'))
    });
});

Also, keep in mind, that this might/will cause problems with rendering in some browsers (i.e. WebKit doesn't resize the input).

http://jsfiddle.net/YTD9p/

0

精彩评论

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