开发者

Get value from checkbox - send it to a form (Hidden field)

开发者 https://www.devze.com 2023-03-30 19:06 出处:网络
How can I pass values from checkboxes in to a form on my website using jQuery? Example When a checkbox is checked, jQuery will MAKE a new input field, inside a specific fo开发者_如何学Crm.

How can I pass values from checkboxes in to a form on my website using jQuery?

Example

When a checkbox is checked, jQuery will MAKE a new input field, inside a specific fo开发者_如何学Crm.

Thanks


$('#checkbox_field_id').click(function() {
    var checkbox_field_value = $(this).val();
    $('#hidden_input_id').val(checkbox_field_value);
});


If you want JQuery to create a new hidden field you can do it like this:

$('#idOfForm').append($('<input>').attr('id', 'idOfHiddenField').attr('value', 'valueOfHiddenField').attr('name', 'nameOfValue').attr('type', 'hidden');


How about this?

$("#myCheckbox").click(function(){
    var $form = $("#myForm");
    var $this = $(this);
    var $hiddenField = $this.data("hiddenField")
    if(!$hiddenField){
        $hiddenField = $form.append('<input type="hidden" />')
        $this.data("hiddenField", $hiddenField);
    };
    $hiddenField.val(this.value);
});

In this code you can be sure the input field only gets appended the first time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号