javascript - 在提交数据之前更改选择表单元素中的值

标签 javascript jquery html forms

尝试更改提交时选择的值。系统不接受“$”或“,”,所以我正在清理数据。

jQuery

jQuery("#vfb-53").val( jQuery("#vfb-53").val().replace(/\D/g,'') );
jQuery("#vfb-55").val( jQuery("#vfb-55").val().replace(/\D/g,'') );

HTML 表单

http://onetrustseniorlending.com/mortgage-loan-calculator

提交时,该字段变为空白,当我检查网络控制台时,该字段为空白。我该如何解决这个问题?

完整代码

$('.visual-form-builder').submit(function(e){

        setCookie('othla',jQuery(".age").val().replace(/\D/g,''),2);
    setCookie('othlv',jQuery(".homeValue").val(),2);
    setCookie('othld',jQuery(".mortgageDebt").val().replace(/\D/g,''),2);

        sim_form$ = $('.crm-form').parent('form');

    sim_form$.attr({'action' : '#'});
    sim_form$.find('.firstname').attr({'name' : 'firstname'});
    sim_form$.find('.lastname').attr({'name' : 'lastname'});
    sim_form$.find('.email').attr({'name' : 'email'});
    sim_form$.find('.phone').attr({'name' : 'phone'});
    sim_form$.find('#vfb-53').attr({'name' : 'cf_1214'});
    sim_form$.find('#vfb-55').attr({'name' : 'cf_1212'});


    sim_form$.prepend('<input type="hidden" name="apipassword" value="0n3trustAPI">');
    sim_form$.prepend('<input type="hidden" name="apimethod" value="create">');
    sim_form$.prepend('<input type="hidden" name="apimodule" value="Leads">');
    sim_form$.prepend('<input type="hidden" name="assigned_user_id" value="2">');
    sim_form$.prepend('<input type="hidden" name="_redirect_url" value="' + document.URL + '#loan-schedule-container' + '">');

    jQuery("#vfb-53").val( jQuery("#vfb-53").val().replace(/\D/g,'') );
    jQuery("#vfb-55").val( jQuery("#vfb-55").val().replace(/\D/g,'') );



        //$('.loan-schedule-container').show();
        //e.preventDefault();
    });

最佳答案

您正在尝试更改 <select> 的值元素,但 select 元素只允许选择预定义的选项作为其值,因此通过尝试更改其值,它会选择不存在的 <option> 这可以通过用输入元素替换 select 元素来解决。

另一个解决方案是让可见的选择仅用于用户输入,但隐藏的输入包含您在代码中使用的“干净”值。

关于javascript - 在提交数据之前更改选择表单元素中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20644078/

相关文章:

html - 使用 Flexbox 对齐高度

javascript - 从电话号码中删除前导 "0"并替换为 +44

javascript - Vue Router推送错误: Avoided redundant navigation to current location

javascript - Sequelize - 将新行添加到表并关联表

javascript - 我可以将菜单项添加到 IOS Webkit 复制/定义弹出菜单吗?

javascript - 查询、排序和替换 DOM 元素

javascript - 基于多个条件和多个数组进行过滤?

jQuery 获取针对 Sinatra 的请求未获取文本

javascript - fullpage.js - 在移动设备上设置 responsiveWidth 和 scrollOverflow 中断页面

javascript - 如何从 JavaScript 执行 EventListener 'click'