最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery - How to output "$(this).val()" within html - Stack Overflow

programmeradmin0浏览0评论

I have this code. I'd like to output input value but it appears as "$(this).val()" in html.

bulkEdit.append('<input type="hidden" name="Post[id][]" value="$(this).val()" /> ');

How should I change this code in order to output the value selected jquery object has?

Thanks,

I have this code. I'd like to output input value but it appears as "$(this).val()" in html.

bulkEdit.append('<input type="hidden" name="Post[id][]" value="$(this).val()" /> ');

How should I change this code in order to output the value selected jquery object has?

Thanks,

Share Improve this question asked Aug 28, 2013 at 23:54 HayatoHayato 5611 gold badge10 silver badges15 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 3

Anything within the double quotes will be outputted as text. To output the javascript result, you need to use concatenation.

bulkEdit.append('<input type="hidden" name="Post[id][]" value="'+$(this).val()+'" /> ');

Use Concatenation. and do move it outside double quotes. Else it will be just as text NOT javascript

It will do the job :

bulkEdit.append('<input type="hidden" name="Post[id][]" value="'+$(this).val()+'" /> ');

:)

Use string concatenation?

bulkEdit.append('<input type="hidden" name="Post[id][]" value="' + $(this).val() + '" /> ');

You seem to be looking for

bulkEdit.append('<input type="hidden" name="Post[id][]" value="'+$(this).val()+'" /> ');

With the exception of events (OnClick, OnMouseOver, etc), the contents of an attribute are interpreted as just text--not Javascript. In order to have the value of your hidden input field contain $(this).val(), you'll need to concatenate the javascript with the HTML. This means you're looking for:

bulkEdit.append('<input type="hidden" name="Post[id][]" value="' + $(this).val() + '" /> ');

You can Use String Concatenation to output your Value,

bulkEdit.append('<input type="hidden" name="Post[id][]" value="' + $(this).val() + '" /> ');

More about String Concatenation: http://www.quirksmode/js/strings.html#conc

发布评论

评论列表(0)

  1. 暂无评论