在javascript函数中返回结果代替false

时间:2017-09-04 05:09:08

标签: javascript jquery bootbox

我想返回结果而不是false。请帮助我,已经尝试使用窗口对象并将其放入变量然后访问。

$(document).ready(function() {
  $('#confirm').on('click', function() {
    bootbox.confirm("Are You Sure ?", function(result) {
      bootbox.alert("Confirm result: " + result)
    });
    return false;
  });
});

3 个答案:

答案 0 :(得分:2)

bootbox.confirm是一个异步操作,不像window.confirm,它会阻止Javascript执行,直到用户做出选择。因此,如果您打算在用户确认或取消后执行某些操作,则可以在回调函数中执行此操作。但是你不能把它作为价值归还。你可以这样做:



function userHasConfirmed() {
  console.log("yay!");
}

$(document).ready(function() {
  $('#confirm').on('click', function() {
    bootbox.confirm("Are You Sure ?", function(result) {
      // The code here is executed only after the user makes a choice.
      bootbox.alert("Confirm result: " + result)
      if (result) {
        userHasConfirmed();
      }
    });

    // The code here does not wait for the user to make a choice.
    console.log("Asked the user to make a choice, but they may or may not have made a choice yet!");
  });
});

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js"></script>

<div id="confirm">Click me!</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您正在使用匿名函数作为事件侦听器中的回调,如果您想对此函数中的结果执行某些操作,或者另一种方法是将函数作为参数传递给on&#34; click& #34;将在点击时调用的事件寄存器。

$(document).ready(function() {

   onClickCallback(e){ 
      //do something here 
   }

   $('#confirm').on('click', onClickCallback);
});

答案 2 :(得分:0)

谢谢大家,我想出了这个。上面的代码就像这样工作

import React, { Component } from 'react';
import classnames from 'classnames';
export default class Perfios extends Component {
  static propTypes = {}
  static defaultProps = {}
  state = {}
constructor(props){
  super(props);

}
componentDidMount(){
  this.myform.submit();
}

  render() {
    const { className, ...props } = this.props;
    return(
    <form ref={myform => this.myform = myform} method="POST" action="https://xyz.in/abc/url-third-party">
         <input ref="inputField" name="id" type="hidden" value="11" />
         <input name="adminId" type="hidden" value="123" />
         <input name="surname" type="hidden" value="xyz" />
         <button className="hidden" type="submit">SUBMIT</button>
         </form>
       );
   }
}