简体   繁体   English

Javascript函数仅在第二次单击时有效

[英]Javascript function works only on second click

I'm trying to get prescription details of particular patient and the corresponding result is displayed in modal window. 我正在尝试获取特定患者的处方详细信息,并且相应的结果显示在模式窗口中。 Problem is first result is obtained only on 2nd click. 问题是只有第二次点击才能获得第一个结果。 On successive click result of previous data is obtained. 连续单击后,将获得先前数据的结果。 Below is my HTML: 以下是我的HTML:

<table class="table">
    <thead>
        <tr class="text-center" style="font-weight: bold">
            <td></td>
            <td>Name</td>
            <td>ID</td>
            <td>Age</td>
            <td>Registered On</td>
            <td>View Prescription</td>
        </tr>
    </thead>
    <tr class="text-center">
        <td><input name="name" type="text"></td>
        <td><input name="id" type="text"></td>
        <td><input name="age" type="text"></td>
        <td><input name="reg_on" type="text"></td>
        <td>
            <button id="view_prescription_from_patient_list" value="register" onclick="view_prescription_from_patient_list(this.value)">
                <span class="glyphicon glyphicon-share"></span>
            </button>
        </td>
    </tr>
    <div class="modal_show"></div>
function view_prescription_from_patient_list(patient_id) {
    var dataString = "get_what=prescription_list_for_patient_list&patient_id=" + patient_id;
    $.ajax({
        type: "GET",
        url: "ajax_get_data.php",
        data: dataString,
        dataType: "html",
        success: function(response) {
            $('.modal_show').append(response);
        }
    })
}

I tried on.click method within javascript but that also has the same problem. 我在javascript中尝试过on.click方法,但是也有同样的问题。

You are passing a value of "register" ( this.value from the button clicked). 您正在传递“注册”值(单击按钮中的this.value )。

Instead use jQuery to connect and handler the click event (delete the inline onclick handler). 而是使用jQuery连接并处理click事件(删除嵌入式onclick处理程序)。

eg 例如

$('#view_prescription_from_patient_list').click(function(){ 
        var patient_id = $(this).closest('tr').find('[name=id]').val();
        var dataString = "get_what=prescription_list_for_patient_list&patient_id=" +  patient_id;
        $.ajax({
           type: "GET",
            url: "ajax_get_data.php",
            data: dataString,
            dataType: "html",
            success: function(response) {
                $('.modal_show').append(response);
            }
        })
    }
});

This will fetch the patent-id from the name="id" TD (which presumably has the ID value - but not shown in your example HTML). 这将从name="id" TD (可能具有ID值-但未在示例HTML中显示)中获取专利ID。

If your code does not follow the DOM elements it references you will also need to wrap it in a DOM ready handler: 如果您的代码未遵循其引用的DOM元素,则还需要将其包装在DOM ready处理程序中:

$(function(){
    // Your code here
});

Note: $(function(){ is just a shortcut for $(document).ready(function(){ 注意: $(function(){只是$(document).ready(function(){

Code with jquery on.('click' and you can get patient_id from the parent td Like $(this).parent().parent().find('input[name=id]').val(); 带有jquery的代码on.('click' ,您可以从父td获得Patient_id,例如$(this).parent().parent().find('input[name=id]').val();

This code may help you to understand 此代码可以帮助您了解

 $('#view_prescription_from_patient_list').on('click',function(){ var patientId = $(this).parent().parent().find('input[name=id]').val(); var dataString = "get_what=prescription_list_for_patient_list&patient_id="+patientId; $.ajax( { type:"GET", url:"ajax_get_data.php", data:dataString, dataType:"html", success:function(response) { $('.modal_show').append(response); } } ) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table"> <thead> <tr class="text-center" style="font-weight: bold"> <td></td> <td>Name</td> <td>ID</td> <td>Age</td> <td>Registered On</td> <td>View Prescription</td> </tr> </thead> <tr class="text-center"> <td><input name="name" type="text"></td> <td><input name="id" type="text"></td> <td><input name="age" type="text"></td> <td><input name="reg_on" type="text"></td> <td><button id="view_prescription_from_patient_list" value="register" ><span class="glyphicon glyphicon-share"></span></button></td> </tr> 

You're reading wrong value as pointed out in previous answers. 您正在读取错误的值,如先前答案中指出的那样。
I'd suggest also to use Unobtrusive JavaScript 我建议也使用Unobtrusive JavaScript

$( document ).ready(function(){

    $('#view_prescription_from_patient_list').on('click', function(){
        var patient_id = $('[name="id"]', $(this).closest('tr')).val(),
            dataString = "get_what=prescription_list_for_patient_list&patient_id="+patient_id;
        $.ajax({
            // ...
        });
    });
});

If you really need to stick to onclick attribute for some reason (althought you shouldn't), pass the button to the method: 如果由于某种原因(虽然您不应该)确实需要坚持使用onclick属性,请将按钮传递给方法:

<button id="view_prescription_from_patient_list" value="register" onclick="view_prescription_from_patient_list(this)">
     <span class="glyphicon glyphicon-share"></span>
</button>

and then in JS: 然后在JS中:

function view_prescription_from_patient_list(btn) {
    var patient_id = $('[name="id"]', $(btn).closest('tr')).val(),
        dataString = "get_what=prescription_list_for_patient_list&patient_id=" + patient_id;
    $.ajax({
        // ...
    });
}

Since you are already using jQuery you can wire the "onclick" event of your button using jQuery. 由于您已经在使用jQuery,因此可以使用jQuery连接按钮的“ onclick”事件。

You need to wire your event handler up like this. 您需要像这样连接事件处理程序。

$(document).ready(function() {
    $('#view_prescription_from_patient_list').on('click', function(e) {
        e.preventDefault();

        var patientId = $(this).parent().parent().find('input[name=id]').val();

        // do your ajax call here...
    });
});

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

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