簡體   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