【问题标题】:jQuery's val() returns empty string on bootstrap popover input fieldjQuery 的 val() 在引导弹出框输入字段上返回空字符串
【发布时间】:2013-11-26 08:53:54
【问题描述】:

我有一个带有一些输入字段的引导弹出表单。我在表单中添加了一个提交按钮,它会触发客户端 JS 验证。但是,当单击按钮时,输入字段的当前值并没有被 jQuery 的 val() 方法捕获:我只是得到一个空字符串。

这里是标记:

<div class="popover fade right in" style="top: -154.5px; left: 249px; display: block;">
    <div class="arrow">
    </div>
    <h3 class="popover-title">New Job Site contact</h3>
    <div class="popover-content">
        <form class="popover-form form-horizontal" id="newjobsite_contact_form" accept-charset="utf-8" method="post" action="http://dev.temperature/home/#">
            <div class="form-group">
                <div class=" required ">
                    <input type="text" class="form-control" id="popover-first_name" required="1" placeholder="First name" value="" name="first_name">
                </div>
                <div class=" required ">
                    <input type="text" class="form-control" required="1" placeholder="Surname" value="" name="surname">
                </div>
                <div class=" required ">
                    <input type="text" class="form-control" required="1" placeholder="Phone" value="" name="phone">
                </div>
                <div class="">
                    <input type="text" class="form-control" placeholder="Mobile" value="" name="mobile">
                </div>
                <div class="">
                    <input type="email" class="form-control" placeholder="Email" value="" name="email">
                </div>
                <div class="">
                    <input type="url" class="form-control" placeholder="Website" value="" name="website">
                </div>
            </div>
            <div class="popover_buttons">
                <button class="btn btn-success" onclick="submit_newjobsite_contact(); return false;" type="button" id="newjobsite_contact_submit">Submit</button>
                <button class="btn btn-warning" onclick="close_newjobsite_contact(); return false;" type="button" id="newjobsite_contact_cancel">Cancel</button>
            </div>
        </form>
    </div>
</div>

这里是 JS:

function submit_newjobsite_contact() {
    errors_found = validate_popover_form($('#newjobsite_contact_form'));

    if (errors_found.length == 0) {
        // Form values submitted to PHP code through AJAX request here 
    } else {
        error_msg = "Please check the following errors:\n";
        $(errors_found).each(function(key, item) {
            error_msg += "- "+item.message+"\n";
        });
        alert(error_msg);
    }
}

function validate_popover_form(form_element) {
    found_errors = [];
    $('span.error').remove();

    form_element.find('select,input').each(function(key, item) {
        if ($(item).attr('required') && $(item).val().length == 0) {
            found_error = true;
            found_errors.push({elementname: $(item).attr('name'), message: "A value for "+$(item).attr('placeholder')+" is required"});
        }

        console.log($(item).val()); // More validation here, just putting debugging code instead
    });
    return found_errors;
}

我做错了什么?这些输入字段的所有其他属性都被 jQuery 正确检索,而不是我在其中输入文本后的值。

【问题讨论】:

  • 奇怪的是,当我把它放到fiddle 中时它工作正常,但它在我自己的页面上不起作用。我猜bootstrap的popover JS在某种程度上弄乱了jquery。

标签: javascript jquery html twitter-bootstrap


【解决方案1】:

这里找不到这个问题的答案,因为我没有贴出整个源码JS,太大了。真正发生的是我不小心克隆了弹出框,导致输入字段重复。

【讨论】:

  • 哦,伙计们,我犯了同样的错误,我花了一段时间才弄清楚我克隆了表单!你的回答帮助了我!谢谢!
【解决方案2】:
form_element.find('select,input').each(function(key, item) {
        if ($(item).attr('required') && $(item).val().length == 0) {
            found_error = true;
            found_errors.push({elementname: $(item).attr('name'), message: "A value for "+$(item).attr('placeholder')+" is required"});
        }

我修改为:

    form_element.find('select,input').each(function(key, item) {
            if ($(this).data('required') == '1' && $(this).val().length == 0) {
                found_error = true;
                found_errors.push({elementname: $(this).attr('name'), message: "A value for "+$(this).attr('placeholder')+" is required"});
}

尝试使用数据属性,所以不要使用required="1",而是使用data-required="1"

<input type="text" class="form-control" required="1" placeholder="Surname" value="" name="surname">

所以你的输入应该是这样的:

<input type="text" class="form-control" data-required="1" placeholder="Surname" value="" name="surname">

【讨论】:

  • 抱歉,这不是问题所在。我正在使用此表单的输入来提交 AJAX 请求。正如您在按钮的 onclick 事件处理程序中看到的那样,它被设置为无论如何都返回 false,因此可以防止正常的提交事件。我想捕获这些输入,在客户端验证它们,然后通过 POST 将它们发送到 PHP 函数。
  • @NicolasConnault 也许你想重新检查你的推杆,我没有看到你提交的return false; 但取消了。我也没有看到 ajax。无论如何我都会为您的 validate_popover_form 提供替代解决方案
  • 你说得对,我在这里复制代码时犯了一个错误。但这并没有改变问题。我删除了 AJAX 调用,因为它与这里的问题无关。由于无论输入的值如何验证都会失败,因此 AJAX 代码永远不会运行。
  • @NicolasConnault 更新了答案。看看能不能用
  • 抱歉,这并没有解决实际问题。使用您的代码或我的代码,“必需”条件按预期工作。这是 val() 方法没有按预期工作(验证代码中的第二个条件)。 $(item).val() 只返回一个空字符串,即使该输入中有文本。我什至通过向元素添加唯一 ID 并将其用作 jquery 选择器进行测试:仍然没有值。
猜你喜欢
  • 2012-06-05
  • 2011-05-08
  • 2023-03-03
  • 2019-03-14
  • 1970-01-01
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多