简体   繁体   English

如何在用户采取行动或在 javaScript 中输入一些值之前停止执行?

[英]How to stop execution until user take action or enter some value in javaScript?

In a button I am calling a function getrequestvalue() which open a model then user enter yes or no.在一个按钮中,我调用 function getrequestvalue()打开 model 然后用户输入是或否。

I want to get these two values 1 is "value" and 2nd either yes or no in my function.我想在我的 function 中获得这两个值 1 是“值”,第二个是或否。

Here is my code which I tried but I am stuck:这是我尝试过的代码,但我被卡住了:

<a onclick='getrequestvalue();' class="btn btn-sm btn-danger" data-id="{{ $customer->id }}" data-toggle="modal" data-target="#delete-customer-modal"><i class="fa fa-trash"></i></a>
              

javascript javascript

function deletecustomer(id){
    var a = getrequestvalue();
    if (id === "yes"){
    $.ajax({
        type: "GET",
        dataType: "json",
        url: '{{ route('delete.customer') }}',
        data: {ids : a}, 
        success: function (data) {
        toastr.success(data.message);
        }
    });
    }
    $("#delete-customer-modal .close").click(); 
}
function getrequestvalue(){
     var param = $(this).data('id');
}

my model我的 model

<div class="modal fade" id="delete-customer-modal" tabindex="-1" role="dialog" aria-labelledby="delete-modal" aria-hidden="true">
                <div class="modal-dialog modal-sm" role="document">
                    <div class="modal-content">
                        <div class="modal-header bg-danger">
                            <h5 class="modal-title" id="delete-modal-label" style="color: white">Are You Sure ?</h5>
                            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">×</span>
                            </button>
                        </div>
                        <div class="modal-body">Are you sure to delete it . Data will be deleted permanently from system.</div>
                        <div class="modal-footer">
                            <button class="btn btn-default" type="button" data-dismiss="modal">No</button>
                            <a onclick="deletecustomer("yes")" class="btn btn-danger f-d" style="color: white">Yes</a>
                        </div>
                    </div>
                </div>
            </div>```

The phrase词组

<a onclick='myfunction("value");'></a>

Simply calls myfunction and gives the first parameter the string value of the word "value", only, nothing special, so the parameter name actually is equal to the string "value", when you check it, seemingly this is unrelated to whether or not the user clicked in the yes or no button简单地调用myfunction ,给第一个参数一个“value”这个词的字符串值,只是,没什么特别的,所以参数名实际上等于字符串“value”,当你检查的时候,看起来这与是否无关用户单击是或否按钮

I don't have the backend code for what makes the buttons work, but in order to get which button is pressed you would have to add some kind of event listener to both buttons, and then call a function that reads the data attributes of each button, or simply pass a boolean value of it being either the yes or no button我没有使按钮起作用的后端代码,但是为了获得按下哪个按钮,您必须向两个按钮添加某种事件侦听器,然后调用读取每个按钮的数据属性的 function按钮,或者只是传递一个 boolean 值,它是是或否按钮

So it would also seem that the yes or no buttons are different anyways, one being an a and the other a button , but essentially you would attach an event to each one, for example, you can add an inline event listener if you want (although generally discouraged):所以看起来 yes 或 no 按钮无论如何都是不同的,一个是a而另一个是 a button ,但基本上你会为每个按钮附加一个事件,例如,如果你愿意,你可以添加一个内联事件侦听器(虽然通常不鼓励):

<button onclick="isYes(false)" class="btn btn-default" type="button" data-dismiss="modal">No</button>
                <a onclick="isYes(true)" class="btn btn-danger f-delete-btn" style="color: white">Yes</a>

Then in JS然后在 JS

function isYes(what) {
//Check if true or false
}


            

You can get value of data-id of a which is clicked inside shown.bs.modal event then assign this value to a tag which is inside your modal.您可以获取在shown.bs.modal事件中单击的data-id a值,然后将此值分配给模式内a标签。 Then, whenever user click Yes just get the attr value and pass same to your ajax call.然后,每当用户单击Yes时,只需获取attr值并将其传递给您的 ajax 调用。

Demo Code :演示代码

 function deletecustomer(el) { console.log("Here--" + $(el).data('id')) $.ajax({ type: "GET", dataType: "json", url: 'your url', data: { ids: $(el).data('id') //pass that here }, success: function(data) { toastr.success(data.message); } }); $("#delete-customer-modal.close").click(); } //on modal show $('#delete-customer-modal').on('shown.bs.modal', function(e) { var data_id = $(e.relatedTarget).data('id') //get attr of `a` tag console.log("Inside this " + data_id) $(this).find('.modal-footer a').data('id', data_id); //add it inside `a` tag modal });
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <a class="btn btn-sm btn-danger" data-id="1" data-toggle="modal" data-target="#delete-customer-modal"><i class="fa fa-trash">Delete</i></a> <a class="btn btn-sm btn-danger" data-id="2" data-toggle="modal" data-target="#delete-customer-modal"><i class="fa fa-trash">Delete</i></a> <div class="modal fade" id="delete-customer-modal" tabindex="-1" role="dialog" aria-labelledby="delete-modal" aria-hidden="true"> <div class="modal-dialog modal-sm" role="document"> <div class="modal-content"> <div class="modal-header bg-danger"> <h5 class="modal-title" id="delete-modal-label" style="color: white">Are You Sure?</h5> <button class="close" type="button" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body">Are you sure to delete it. Data will be deleted permanently from system.</div> <div class="modal-footer"> <button class="btn btn-default" type="button" data-dismiss="modal">No</button> <a onclick="deletecustomer(this)" class="btn btn-danger fd" style="color: white">Yes</a> </div> </div> </div> </div>

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

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