简体   繁体   中英

Build “value” for text area base on input and checkbox

As the question said, how can I build a context for a textarea , say I don't want or I can't modified the controller or the models of a Framework, but I can change the or add fields to the "views/templates", so say this is my template:

<form>
    <input name="name" id="name">
    <input name="lastname" id="lastname">
    <textarea name="onservations" id="observations"></textarea>
</form>

Say thats all there is for the form, but I need to add a few other inputs but the extra input are not defined on the controller or model and they also need to be save and I only know a bit of HTML so please bear with me on this one...

So since I can not add or modifid the controller or model, I can only assume that I can add my extra field to the template and then pass them along to the textarea in basic html since the framewok wont strip the basic html tags, I can use p , em , a , b , strong , i , table , img ... so my new form will be like this:

<form>
    <input name="name" id="name">
    <input name="lastname" id="lastname">
    <div id="original_text" style="display: none;">
        <textarea name="observations" id="observations"></textarea>
    </div>

    <!-- Extra Field -->
    <input type="text" name="what">
    <input type="radio" value="Yes" id="yes_r" name="n_rs">Yes
    <input type="radio" value="No" id="no_r" name="n_rs" >No
    <textarea name="observations_2" id="observations_2"></textarea>
</form>

So there are my extra field, now, how can I pass the values of the extra fields to the textarea with the ID "observations" say the user checks Yes and in "What" he/she type something, and then on the "observations_2" type something else...
so the value for the original textarea would be something like this:

<p>What happen?: [value from input text name what]</p>
<p>Would you like a candy? [value from the radio buttons named n_rs]</p>
<p>Your observations are: [value from textarea named observations]</p>

And those 3

get saved on the data base under the column "observations", now, remember, the controllers or models or helpers can not be modified I can not add other columns to the data base so I have to work with what I have, and my only solution is to do a jquery that can pass those values to that textarea...
So how is the jquery would look like?
Thank for taking the time.

This should concatenate your values, then put the formatted string into your textarea . It will "refresh" your textarea if any of the elements change:

$('#observations_2, input[name=what], input:radio[name=n_rs]').on("keyup change", function(){
    var allTogether = 
        "What happen?: " + 
        $('input[name=what]').val() +
        "\n" +
        "Would you like a candy? " +
        $('input:radio[name=n_rs]:checked').val() +
        "\n" +
        "Your observations are: " +
        $('#observations_2').val()
    ;
    $('#observations').val(""); // First clear it
    $('#observations').val(allTogether); // Then rebuild it
});

Fiddle: http://jsfiddle.net/QWau9/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM