繁体   English   中英

通过javascript和jquery获取.val元素的div元素

[英]Getting div elements by .val element with javascript and jquery

我有一个脚本,一旦点击下拉框,就会生成div ID,如下所示。 div id实际上是由item id的值生成的。 我可以获得价值,但不知道如何添加它。 所以基本上它应该像$('#itemsupdate2').load('index.php / sales #item`(item id在这里...

我无法让它发挥作用。 我可以通过添加来获得价值

var item = $('input [name = item]')。fieldValue();

这工作,我可以在警告框中获取值。 我不知道如何使用此值进行加载。 我想要做的是如下所示。

$('#itemsupdate2')。load('index.php / sales #item“+ item [0] +”')。fadeIn('slow');

<div class="itemsupdate">

<div id="item1">
sample text 1   
</div>

<div id="item8">
1guiness150.004 
</div>

<div id="item9">
1999999.009 
</div>

“.val()”方法与“id”值无关。 你要找的是“.attr()”:

var theId = $(whatever).attr('id');

现在,你还有另外一个问题。 “id”值需要看起来像标识符。 因此,“9”将不起作用。 你可以使用像“_9”之类的东西。

$('select').bind('change', function() {
    var val = $(this).val();
    $('.itemsupdate').html('<div id="' + val +'">contents of the div</div>');
});

这会将div添加到div.itemsupdate

然后您可以像这样访问它: $('#' + $('select').val());

.val()返回所选对象的值。 这适用于textareas和输入,但不适用于div。 使用.text()代替获取文本元素,或使用.html()获取div中的完整html结构。 在您的示例中,您使用ID来标识您的div。 那么你为什么不使用这个id选择器#?

$('#1').text(); // returns 'sample text 1'
$('#1').text('new text'); // will change the divs content with 'new text'
$('#1').text('text before '+$('#1').text()+' text after') // will insert 'text before' before the content and add 'text after' after the divs content: text before sample text 1 text after

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM