开发者

jQuery: having radio buttons and a textarea update hidden form fields

开发者 https://www.devze.com 2023-04-07 14:55 出处:网络
Really new to using jQuery and trying to find an example I need. 1) if I have, say, 5 radio buttons to choose an item, how do I pass the selected item to a hidden form field?

Really new to using jQuery and trying to find an example I need.

1) if I have, say, 5 radio buttons to choose an item, how do I pass the selected item to a hidden form field?

2) same question for a textare开发者_开发技巧a. How do I pass the text written to a hidden form field and make sure it's escaped safely for a form submission?

Thanks for any help.


You can just bind to the change event:

<input type="hidden" id="myradiovalue" />
<input type="radio" name="myradio" value="0" />
<input type="radio" name="myradio" value="1" />

$('input[name=myradio]').change(function() {
    $('#myradiovalue').val($(this).val());
});

And almost the same for textarea:

<input type="hidden" id="mytextarevalue" />
<textarea id="mytextareavalue"></textarea>

$('textarea').change(function() {
    $('#mytextareavalue').val($(this).val());
});


For both <input type="radio"> and <textarea>, you will want to use jQuery change() method. If you want to sanitize the input before it is inserted into a <input type="hidden"> then you will need to use some regex or a library that does it for you, like jQuery Validation Plugin. Keep in mind that any sanitation/validation you do with javascript/jQuery will need to be double-checked server-side after the form is submitted.

But I don't know why you are copying data from one form input to another, can't you just use the form input as it is? What is the point of having the data in both a <textarea> and a <input type="hidden">?

0

精彩评论

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

关注公众号