开发者

Take text from Text area

I have this html code -

<form method="post" style="width:100%; margin-bottom:40px;" >
                <textarea id="wall_post" style="width:100%;; margin-left:-4px; margin-right:auto; resize:none;" rows="4"></textarea>
                <input id="wallPost" type="submit" value="Post" method="post" style="float:right; font-size:65%;" />
                <div id="vectors" style="display:inline; float:left; margin-right:20px;">
                    <input type="checkbox" id="sms" value = "SMS" method = "post"/><label for="sms" style="font-size:65%;">SMS</label>
                    <input type="checkbox" id="email" value = "Email" method = "post"/><label for="email" style="font-size:65%;">Email</label>

                </div>  
            </form>

As you can see there are a few buttons. I'd like to have it so that when say the checkbox with value SMS is clicked, the开发者_C百科 stuff in the textarea is passed to a django view. I know I can pass stuff into django using a jquery ajax post with the relevant url conf, but how do I do so on the request of the user? (in pseudocode - if user clicks post, give django function text area text)

thanks!


Why do you have to change whether or not your textarea is being submitted to django?

It's not much data. Why not always submit it and check in your view if the SMS checkbox is checked? If checked: do something with textarea data. If not: don't.

Note: you're missing the name attribute on each of your inputs.

def my_view(request):
    if request.method == 'POST':
        if request.POST.get('sms'):
            # do something with text area data since SMS was checked
            print request.POST.get('my_textarea')

        # process form as usual.

If you want to change whether or not django reads your data, you could potentially dynamically add or remove the textarea's name attribute with javascript, but this is riskier and more work than the above solution.

update - the html to work with above code.

<form method="post">
    {% csrf_token %}
    <textarea name="my_textarea"></textarea>
    <input type="checkbox" name="sms" />
</form>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜