简体   繁体   English

如何在javascript中获取inner div的值

[英]how to get the value of inner div in javascript

I want to access the inner div and get it's value using Javascript. 我想访问内部div并使用Javascript获得其价值。 Would you please help me, I want to get the value of "phone" and insert it into a "list". 您能帮我吗,我想获取“电话”的值并将其插入“列表”中。 I don't how to loop in all the DIVS and get only the checked one's below my setup: 我不会在所有DIVS中循环播放,而只在我的设置下获取选中的内容:

<div class="headings_01">
<div class="checkbox_01"><input name="contact_id" id="selectedcontacts" type="checkbox" value="175" style="margin-top:0px;" /></div>
<div class="firstname_01" name="fname">|175|James</div>
<div class="lastname_01" name="lname">James</div>
<div class="group_01">G1</div>
<div class="mobile_nmbr_01" name="phone" id="phone">123456478</div>
</div>

I have tried this and did not success: 我已经尝试过但没有成功:

function AddContactPhoneNo()
{   
    var recipientNumber = document.getElementById("phone");
    var opt = "<option value='" + recipientNumber.value + "'>" + recipientNumber.value + "</option>"

    if  (recipientNumber.value != "")
    {
         $('#selectedOptions').append(opt);

            recipientNumber.value = "";

    }       
} 

I think i'm late responser about the udate of my answer but i'm sure if you find even now the solution then it will help you much. 我认为我对我的答复的及时性是迟到的答复,但我敢肯定,即使您现在找到了解决方案,它也会对您有很大帮助。 You should change your HTML first then write the jQuery code as like following block of code. 您应该先更改HTML,然后像下面的代码块一样编写jQuery代码。

HTML 的HTML

<div class="headings_01">
   <div class="checkbox_01">
     <input name="contact_id[]" type="checkbox" value="175" style="margin-top:0px;" />
   </div>
   <div class="firstname_01" name="fname">|175|James</div>
   <div class="lastname_01" name="lname">James</div>
   <div class="group_01">G1</div>
   <div class="mobile_nmbr_01">+8801711240984</div>
</div>

jQuery jQuery的

function AddContactPhoneNo()
{ 
    $("input[name='contact_id[]']:checked").each(function(){

        var opt_value = $(this).parent().parent().children('.mobile_nmbr_01').text();
        var opt = "<option value='" + opt_value + "'>" + opt_value + "</option>";

        if  (opt_value != "")
        {
             $('#selectedOptions').append(opt);
        }
    });
}

Thats all. 就这样。 Very simple. 很简单。

If you have a number of such repeating phone divs 如果您有很多这样的重复电话div

set a counter value 设置一个计数器值

var counter = 0;
var phoneDiv = "phone" + counter;

var a = document.getElementById(phoneDiv).innerHTML

counter++;

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

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