简体   繁体   English

bootstrap data-id在javascript中未定义

[英]bootstrap data-id undefined in javascript

I am trying to get value of data-id binded to modal. 我试图获取绑定到模式的data-id的值。 Here is code 这是代码

<i class="fa fa-envelope" aria-hidden="true" data-target="#btnSendSMS" data-toggle="modal" data-id="ISBN564541"></i>

//popup model //弹出模型

   <div id="btnSendSMS" style="display: none" class="modal fade TransParentAll" role="dialog">
        <div class="modal-dialog" id="C_Model_Dialog">
            <div class="modal-content" id="C_Model_Content">
                <div class="modal-header" id="C_Model_Header">
                    <h4 class="modal-title">Send SMS</h4>
                </div>
                <div class="modal-body" id="C_modal_body">
                    <form class="AddFreelancerForm">
                        <div class="form-group">
                            <span class="pull-left">SMS</span>
                            <input type="text" id="textSMS" class="form-control" placeholder="write something here" />
                        </div>
                    </form>
                </div>
                <div class="modal-footer" id="C_Model_Footer">
                    <button id="btnsave" type="button" class="btn c_G_Btn pop_upSaveBtn" value="1" onclick="SendSMS()">
                        <i class="fa fa-floppy-o" aria-hidden="true"></i>
                        Send
                    </button>
                    <button id="btnCancel" type="button" class="btn c_R_Btn" data-dismiss="modal" value="1" onclick="CloseWindow()">
                        <i class="fa fa-times" aria-hidden="true"></i>
                        Close
                    </button>
                </div>
            </div>
        </div>
    </div>

//JS // JS

function SendSMS() {
        var candidateId = $(this).data('id');
        var can=$(this).attr("data-id") ;

But i am getting undefined in both values on button click. 但是在单击按钮时,两个值都变得不确定。

As you wrote click event on <button> tag, $(this) will always refers to <button> object. 当您在<button>标签上编写click事件时, $(this)将始终引用<button>对象。 So, var can=$(this).attr("data-id") ; 因此, var can=$(this).attr("data-id") ; will always return undefined since <button> object don't have attribute called data-id 由于<button>对象没有名为data-id的属性,因此将始终返回未定义

if you are trying to get id of <button> object, try below line 如果您尝试获取<button>对象的ID,请尝试以下行

var candidateId = $(this).attr('id');

if <i class="fa fa-envelope" aria-hidden="true" data-target="#btnSendSMS" data-toggle="modal" data-id="ISBN564541"></i> is inside <button> object, you can get data-id with line: 如果<i class="fa fa-envelope" aria-hidden="true" data-target="#btnSendSMS" data-toggle="modal" data-id="ISBN564541"></i>位于<button>对象,您可以通过以下行获取data-id

var can=$(this).find("i").attr("data-id") ;

ie your code will be like, 即您的代码将像

function SendSMS() {
        var candidateId = $(this).attr('id');
        var can=$(this).find("i").attr("data-id") ;
}

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

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