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

Changing value in select form element before data is submitted

本文关键字:元素 表单 选择 数据 提交      更新时间:2023-09-26

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

jQuery

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

网页表单

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>元素的值,但选择元素仅允许选择预定义的选项作为其值,因此通过尝试更改其值,它选择不存在的<option>这可以通过将选择元素替换为输入元素来解决。

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

我认为问题是尝试将选择的值设置为不在选项集中的值将导致未定义或空字符串。