javascript - 按钮不触发 Ajax

标签 javascript jquery html ajax

我有一个通过 AJAX 向数据库提交元素的表单,我遇到的问题是,当我单击提交按钮时,它不执行任何操作,也不会调用 ajax 请求或任何其他操作。

HTML

  <div class="list">
    <input type="hidden" id="id" value="" />
    <div class="item">
        <label>Name</label>
        <input type="text" id="name" value="" />
    </div>
    <div class="item">
        <label>Number</label>
        <input type="text" id="number" value="" />
    </div>
    <div class="item">
        <label>Address</label>
        <input type="text" id="address" value="" />
    </div>
     <div class="item">
        <label>Price</label>
        <input type="text" id="price" value="" />
    </div>
     <div class="item">
        <label>Deposit</label>
        <input type="text" id="deposit" value="" />
    </div>
     <div class="item">
        <label>Product</label>
        <input type="text" id="product" value="" />
    </div>
     <div class="item">
        <label>Payment Type</label>
        <input type="text" id="payment_type" value="" />
    </div>
     <div class="item">
        <label>Deal Date</label>
        <input type="text" id="deal_date" value="" />
    </div>
     <div class="item">
        <label>Install Date</label>
        <input type="text" id="install_date" value="" />
    </div>
     <div class="item">
        <label>Installed</label>
        <input type="text" id="installed" value="" />
    </div>
     <div class="item">
        <label>Notes</label>
        <textarea name="notes" cols="" id="price" rows=""></textarea>
    </div>
     <div class="item">
        <label>Contract Received</label>
        <input type="text" id="contract_recieved" value="" />
    </div>
    <div class="item">
        <input type="button" id="insert" class="button button-block" value="Insert" />
    </div>

Javascript

  $(document).ready(function() {
    $("#insert").click(function() {
        var name = $("#name").val();
        var number = $("#number").val();
        var address = $("#address").val();
        var price = $("#price").val();
        var deposit = $("#deposit").val();
        var product = $("#product").val();
        var payment_type = $("#payment_type").val();
        var deal_date = $("#deal_date").val();
        var install_date = $("#install_date").val();
        var installed = $("#installed").val();
        var notes = $("#notes").val();
        var contract_recieved = $("#contract_recieved").val();
        var dataString = "name=" + name + "&number=" + number + "&address=" + address + "&price=" + price + "&deposit=" + deposit + "&product=" + product + "&payment_types=" + payment_types + "&deal_date=" + deal_date + "&install_date=" + install_date + "&installed=" + installed + "&notes=" + notes + "&contract_recieved=" + contract_recieved + "&insert=";
        if ($.trim(title).length > 0 & $.trim(duration).length > 0 & $.trim(price).length > 0) {
            $.ajax({
                type: "POST",
                url: "http://http://www.domain.com/test6/services/insert.php",
                data: dataString,
                crossDomain: true,
                cache: false,
                beforeSend: function() {
                    $("#insert").val('Connecting...');
                },
                success: function(data) {
                    if (data == "success") {
                        alert("inserted");
                        $("#insert").val('submit');
                    } else if (data == "error") {
                        alert("error");
                    }
                }
            });
        }
        return false;
    });
});

最佳答案

在网址中使用“https://www.domain.com/test6/services/insert.php” 此外,您使用的一些变量未定义,请修复它们,例如 payment_types 和持续时间。

关于javascript - 按钮不触发 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40338439/

相关文章:

jquery - 向 jquery 计数器添加逗号

javascript - 使用 jquery 递增

html - CSS:当用户向上调整它的大小时如何让滚动条响应容器内的div

javascript - 为什么我使用 JSON P api 会收到 404 Forbidden 错误?

javascript - 每次单击后页面重新加载时,如何计算按钮单击的次数?

javascript - 如何从 json 数据标记 Highcharts 工具提示?

javascript - JS 表单验证问题

Javascript 如何按值从 URL 字符串中删除参数?

javascript - 为什么高斯-勒让德算法第三次迭代后 Pi 的值保持不变?

javascript - 如何使用带有 Angular js 和 Bootstrap 的 "Cancel"按钮关闭模态