第一个功能必须在调用第二个功能之前完成

时间:2018-07-21 22:50:58

标签: javascript

在我的示例中,我有三个函数必须完全执行才能进入下一个函数。我知道我可以将第二个功能放在第一个功能中,等等,但是我需要灵活地以各种顺序执行功能。

进行研究后,我似乎需要进行“回调”,但不了解如何将其应用于我的情况。这是我到目前为止所拥有的。当然,它会通过所有三个功能,而不会停止警报。有人可以在这里帮助我,或者至少指出我应该考虑的方向吗?

<!DOCTYPE html>
<html>
<body>
    <h3>Validates Input Values</h3>
    <form name='checkout_form'>
        <table id="table_form">
            <tr>
                <td>First Name:</td>
                <INPUT type="text" id="xFirstName" STYLE="color: #3366CC; text-align: left; font-family: Times; font-size: 12px; background-color: transparent; border:dotted; border-color:#7b0000; border-width:1px; width:180px" name="first_name" value="" size="14" onKeyup="autotab(this,document.donate_form.last_name)"
                    maxlength=14 onkeypress="return inputLimiter(event,'NameCharacters')">
                </td>
            </tr>
            <tr>
                <td>Last Name:</td>
                <td>
                    <INPUT type="text" id="xLastName" STYLE="color: #3366CC; text-align: left; font-family: Times; font-size: 12px; background-color: transparent; border:dotted; border-color:#7b0000; border-width:1px; width:180px" name="last_name" value="" size="14" onKeyup="autotab(this,document.donate_form.amount)"
                        maxlength=14 onkeypress="return inputLimiter(event,'NameCharacters')">
                </td>
            </tr>
            <tr>
                <td>Zip Code:</td>
                <td>
                    <INPUT type="text" id="xZipCode" STYLE="color: #3366CC; text-align: left;  
font-family: Times; font-size: 12px; background-color: transparent; border:dotted; border-color:#7b0000; border-width:1px; width:180px" name="zip" value="" size="14" onKeyup="autotab(this,document.donate_form.amount)" maxlength=14 onkeypress="return inputLimiter(event,'Numbers')">
                </td>
            </tr>
        </table>
    </form>
    <button onclick="myFunction_Varify()">VALIDATE</button>
    <script>
    function myFunction_Varify() {
      myFunction_firstName();
      myFunction_lastName();
      myFunction_zipCode();
    }

    function myFunction_firstName() {
      vFirstName = document.getElementById("xFirstName").value;
      if (vFirstName == "") {
        document.forms['checkout_form'].elements['first_name'].focus();
        alert("No FIRST NAME entered");
      }
    }

    function myFunction_lastName() {
      vLastName = document.getElementById("xLastName").value;
      if (vLastName == "") {
        document.forms['checkout_form'].elements['last_name'].focus();
        alert("No LAST NAME entered");
        return false;
      }
    }

    function myFunction_zipCode() {
      vZipCode = document.getElementById("xZipCode").value;
      if (vZipCode == "") {
        document.forms['checkout_form'].elements['zip'].focus();
        alert("No ZIP CODE entered");
        return false;
      }
    }
    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您是&&他们

import UIKit

class DetailVCViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    let hotel = Hotel()
    var myDescription = ""

    @IBOutlet weak var myCollection: UICollectionView!

    @IBOutlet weak var label: UILabel!

    @IBOutlet weak var hotelDescription: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        hotelDescription.text = myDescription
    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return hotel.array.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = myCollection.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! CollectionCellCollectionViewCell

        cell.cellImage.image = UIImage(named: hotel.array[indexPath.row][indexPath.row])

        return cell
    }

    @IBAction func reservar(_ sender: UIButton) {

    }


}


import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var myTable: UITableView!

    let hotel = Hotel()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let cell = UINib(nibName: "TableViewCell", bundle: nil)
        myTable.register(cell, forCellReuseIdentifier: "tableCell")

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return hotel.hotelNames.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = myTable.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! TableViewCell

        cell.hotelName.text = hotel.hotelNames[indexPath.row]
        cell.hotelImage.image = UIImage(named: hotel.hotelImages[indexPath.row])
        cell.hotelImage.contentMode = .scaleToFill
        return cell

    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 250
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "mySegue", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let index = myTable.indexPathForSelectedRow
        let indexNumber = index?.row
        let secondVC = segue.destination as! DetailVCViewController
        secondVC.myDescription = hotel.description[indexNumber!]

    }

}
相关问题