开发者

subtract div text from input

I have a div block with text inside:

<div>hello</div>

I have an input text field whose last entry is always the text inside the div. For example,

<input type="text"></input> with the user inputed text as "bob, james, hello"

On click with jquery, how ca开发者_JAVA技巧n I make the text inside the div be subtracted from the string inside the input, leaving the input as:

<input type="text"></input> text as "bob, james," 


Your syntax is off a litte:

<div>hello</div>
<input type="text" value="bob, james, hello" />

JQuery:

$('div').click(function(){
    var _text = $('input[type=text]');
    _text.val(_text.val().replace($(this).html(),''));
});

Working demo: http://jsfiddle.net/AlienWebguy/kCWtr/1/


Use:

$('div').click(function(){
    var inputElem=$('input[type=text]'), arrText=inputElem.val().split(/,\s*/g), indexElemToRemove=arrText.indexOf($(this).val());
    if(indexElemToRemove>-1) arrText.splice(indexElemToRemove,1);
    inputElem.val(arrText.join(', '));
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜