简体   繁体   English

根据输入和复选框为文本区域构建“值”

[英]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: 如问题所述,如何为textarea构建上下文,例如,我不希望或无法修改Framework的控制器或模型,但是可以更改“视图/模板”或在“视图/模板”中添加字段”,所以说这是我的模板:

<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... 说这就是表单的全部内容,但是我需要添加一些其他输入,但是额外的输入未在控制器或模型上定义,它们也需要保存,并且我只知道一点点HTML,所以请多多包涵在这个...

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: 因此,由于我无法添加或修改控制器或模型,因此只能假设我可以将额外的字段添加到模板中,然后将它们传递到基本html中的textarea中,因为framewok不会剥离基本html标签,因此我可以使用pemabstrongitableimg ...这样我的新形式将如下所示:

<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... 因此,这里有我的额外字段,我如何将额外字段的值传递给ID为“ observations”的文本区域,说用户选中“是”,然后在“什么”中输入内容,然后在“ observations_2”上输入“输入其他内容...
so the value for the original textarea would be something like this: 因此原始textarea的值将如下所示:

<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 那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... 保存在数据库中“观察”列下,现在,请记住,控制器,模型或助手无法修改,我无法在数据库中添加其他列,因此我只能使用已有的东西解决方案是做一个jQuery,可以将这些值传递到该textarea。
So how is the jquery would look like? 那么,jquery的外观如何?
Thank for taking the time. 感谢您抽出宝贵的时间。

This should concatenate your values, then put the formatted string into your textarea . 这应该连接您的值,然后将格式化的字符串放入textarea It will "refresh" your textarea if any of the elements change: 如果任何元素发生更改,它将“刷新”您的textarea

$('#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/ 小提琴: http//jsfiddle.net/QWau9/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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