html 具有DEFAULT VALUE的可见表单字段上的DTR - 如果URL没有任何参数P.S。:必须为每个字段设置默认值Th

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 具有DEFAULT VALUE的可见表单字段上的DTR - 如果URL没有任何参数P.S。:必须为每个字段设置默认值Th相关的知识,希望对你有一定的参考价值。

<!-- START code that prefills email field with the value of email parameter -->

<script>
    $(document).ready(function() {

        //define get param value function
        function getParameterByName(name, url) {
            if (!url) url = window.location.href;
            name = name.replace(/[\[\]]/g, "\\$&");
            var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                results = regex.exec(url);
            if (!results) return null;
            if (!results[2]) return '';
            return decodeURIComponent(results[2].replace(/\+/g, " "));
        };
        //define function that replaces the form field's placeholder and value with the value of a parameter
        function replaceWithParamValue(label, param) {
            //get the value of the parameter
            var paramValue = getParameterByName(param);
            var $field = ijQuery("[name='" + btoa(label) + "']");
            var $inputHolder = $field.closest('.input-holder');
            //set the placeholder and value for each form field
            if (paramValue != "" && paramValue != undefined) {
                if ($inputHolder.hasClass('field-text')) {
                    $field.attr('placeholder', paramValue)
                        .val(paramValue);
                } else if ($inputHolder.hasClass('field-select')) {
                    $field.find('option[value="' + paramValue + '"]').attr('selected', 'selected');
                }
            };
        }
        //define arrays with parameter - field values
        var parametersNamesArr = ['first_name', 'last_name', 'email', 'country', 'program'];
        var fieldLabelsArr = ['First name', 'Last name', 'Email', 'Country', 'Which program are you interested in?'];
        setTimeout(function() {
            //iterate through the arrays and replace the field values with the parameter values
            for (var i = 0; i < parametersNamesArr.length; i++) {
                replaceWithParamValue(fieldLabelsArr[i], parametersNamesArr[i]);
            }
        }, 1000);
    });
</script>
<!-- END code that prefills email field with the value of email parameter -->
<!-- START code that prefills email field with the value of email parameter -->
<script>
    $(document).ready(function() {
        //define get param value function
        function getParameterByName(name, url) {
            if (!url) url = window.location.href;
            name = name.replace(/[\[\]]/g, "\\$&");
            var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                results = regex.exec(url);
            if (!results) return null;
            if (!results[2]) return '';
            return decodeURIComponent(results[2].replace(/\+/g, " "));
        };

        //define function that replaces the form field's placeholder and value with the value of a parameter
        function replaceWithParamValue(label, param) {
            //get the value of the parameter
            var paramValue = getParameterByName(param);
            //set the placeholder and value for each form field
            if (paramValue != "" && paramValue != undefined) {
                ijQuery("input[name='" + btoa(label) + "']").unbind();
                ijQuery("input[name='" + btoa(label) + "']").attr('placeholder', paramValue);
                ijQuery("input[name='" + btoa(label) + "']").val(paramValue);
            };
        }

        //define arrays with parameter - field values
        var parametersNamesArr = ['firstname', 'lastname', 'company', 'email', 'phone', 'gassafe'];
        var fieldLabelsArr = ['First name', 'Last name', 'Company Name', 'Email', 'Phone Number', 'Gas Safe Number'];

        setTimeout(function() {
            //iterate through the arrays and replace the field values with the parameter values
            for (var i = 0; i < parametersNamesArr.length; i++) {
                replaceWithParamValue(fieldLabelsArr[i], parametersNamesArr[i]);
            }
        }, 2000);
    });

</script>
<!-- END code that prefills email field with the value of email parameter -->
<!-- START code that prefills email field with the value of email parameter -->
   <script>
    $(document).ready(function() {
        setTimeout(function() {
            //define get param value function
            function getParameterByName(name, url) {
                if (!url) url = window.location.href;
                name = name.replace(/[\[\]]/g, "\\$&");
                var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                    results = regex.exec(url);
                if (!results) return null;
                if (!results[2]) return '';
                return decodeURIComponent(results[2].replace(/\+/g, " "));
            };
            //use function to get the value of each parameter
            var fieldLabel = 'Email';
            var emailParam = getParameterByName('email');
            //set the placeholder and value for each form field
            if (emailParam != "" && emailParam != undefined) {
                ijQuery("input[name='" + btoa(fieldLabel) + "']").unbind();
                ijQuery("input[name='" + btoa(fieldLabel) + "']").attr('placeholder', emailParam);
                ijQuery("input[name='" + btoa(fieldLabel) + "']").val(emailParam);
            };
        }, 1000);
    });

</script>
<!-- END code that prefills email field with the value of email parameter -->
$(document).ready(function () {
    setTimeout(function () {
        //define get param value function
        function getParameterByName(name, url) {
            if (!url) url = window.location.href;
            name = name.replace(/[\[\]]/g, "\\$&");
            var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                results = regex.exec(url);
            if (!results) return null;
            if (!results[2]) return '';
            return decodeURIComponent(results[2].replace(/\+/g, " "));
        };
        //use function to get the value of each parameter
        var name = getParameterByName('nombre');
        var email = getParameterByName('email');
        var phone = getParameterByName('phone');
        var city = getParameterByName('city');
        //set the placeholder and value for each form field
        if (name != "" && name != undefined) {
            ijQuery("input[placeholder*='Nome']").unbind();
            ijQuery("input[placeholder*='Nome']").attr('placeholder', name);
            ijQuery("input[placeholder*='Nome']").val(name);
        };
    }, 2000);
});
<script type="text/javascript">
    var tagify = "INSTAPAGE_ALLOW_TAGIFY"
</script>
<script>
    var hasUrlParams = function() {
        var hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
          	if (hash[0] === 'email' && hash[1]) {
            	return true;
              	break;
            };
        };
      	return false;
    };
  
  	var urlParamsTrue = hasUrlParams();
        
    $("document").ready(function() {
      	if(urlParamsTrue ===false ) {
        	setTimeout(function() {
              $("input[name^='W']").each(function() {
                  $(this).attr("placeholder", 'Email');
              });
          }, 2000);
        } else {
          setTimeout(function() {
              $("input[name^='W']").each(function() {
                  var placeHolder = $(this).attr("placeholder");
                  $(this).val(placeHolder);
              });
          }, 2000);
        };
    });
</script>

以上是关于html 具有DEFAULT VALUE的可见表单字段上的DTR - 如果URL没有任何参数P.S。:必须为每个字段设置默认值Th的主要内容,如果未能解决你的问题,请参考以下文章

Drupal 自定义表单记住文本字段数据

不可见的 google Recaptcha 和 ajax 表单

移出可见的 uitableView 后,UITableViewCell 具有灰色背景

访问 - 使用父表单上的按钮使子表单字段不可见:)

具有隐藏可见性的 C++ 模板参数问题

动态表单Angular2。条件字段可见性