简体   繁体   English

带单选按钮的jQuery post函数

[英]jQuery post function with radio buttons

In my Django app, I would like to use Twitter bootstrap radio buttons. 在我的Django应用中,我想使用Twitter引导单选按钮。 Then I would like to post the value of those buttons in order to create an object. 然后,我想发布这些按钮的值以创建一个对象。

Here is my buttons: 这是我的按钮:

  <div class="btn-group" data-toggle="buttons-checkbox">
  <button type="button" class="btn" name="supporting">Team1</button>
  <button type="button" class="btn" name="supporting">Team2</button>
  </div>


  <div class="span6 btn-group ib" data-toggle="buttons-radio">
       <button type="button" class="btn btn-primary active" name="style">relaxed</button>
       <button type="button" class="btn btn-primary active" name="style">strict</button>
  </div>



  <input type="submit" value="create your match">

I would like to get the information from those radio button and create an object match with it in a view: 我想从那些单选按钮获取信息,并在视图中创建与之匹配的对象:

   def test(request):
       user=request.user
       if request.method == 'POST':
       style = request.POST['style']
       supporting = request.POST['supporting']
       match = Match.objects.create(user=user, style=style, supporting=supporting)
     return HttpResponse(test)

The thing is that I don't know JavaScript well, so I didn't find how to get the value from the button. 问题是我不太了解JavaScript,所以我没有找到如何从按钮获取值的方法。

Then, I think I have to use: 然后,我认为我必须使用:

 $.post('/sportdub/points_dub/', {'style': style , 'supporting': supporting});

But how can I do so that style and supporting correspond to the value of the buttons, and then to post it only when the user clicks on the button? 但是,我该如何使样式和支撑与按钮的值相对应,然后仅在用户单击按钮时才发布它?

Thank you very much for your help. 非常感谢您的帮助。

taking my last answer , let's use your own code... in your HTML you have something like: 拿出我的最后一个答案 ,让我们使用您自己的代码...在HTML中,您会看到以下内容:

<form action="/sportdub/points_dub/" method="post" class="form-horizontal">

  <div class="btn-group" data-toggle="buttons-checkbox">
    <button type="button" class="btn" name="supporting">Team1</button>
    <button type="button" class="btn" name="supporting">Team2</button>
  </div>

  <div class="span6 btn-group ib" data-toggle="buttons-radio">
    <button type="button" class="btn btn-primary active" name="style">relaxed</button>
    <button type="button" class="btn btn-primary active" name="style">strict</button>
  </div>

  <input type="submit" value="create your match" />

</form>

and you just have to add one hidden field per block, in your case, add 2. Your form would then match: 并且您只需要为每个块添加一个隐藏字段,就您而言,添加2。您的表单将匹配:

<form action="/page" method="post" class="form-horizontal">

  <input type="hidden" id="supporting_team" value="" />
  <input type="hidden" id="supporting_type" value="" />

  <div class="btn-group supporting_team" data-toggle="buttons-checkbox">
    <button type="button" class="btn" name="supporting">Team1</button>
    <button type="button" class="btn" name="supporting">Team2</button>
  </div>

  <div class="span6 btn-group ib supporting_type" data-toggle="buttons-radio">
    <button type="button" class="btn btn-primary active" name="style">relaxed</button>
    <button type="button" class="btn btn-primary active" name="style">strict</button>
  </div>

  <input type="submit" value="create your match" />

</form>

add a jQuery helper that will set the values of those hidden fields upon buttons click: 添加一个jQuery帮助器,它将在按钮单击时设置这些隐藏字段的值:

// whenever a button is clicked, set the hidden helper
$(".supporting_team .btn").click(function() {
    $("#supporting_type").val($(this).text());
}); 
$(".supporting_type .btn").click(function() {
    $("#supporting_team").val($(this).text());
}); 

when you click the create your match the entire form will be posted to the page set as the action attribute of the form, you have to do nothing more... 当您单击“ create your match ,整个表单将发布到设置为表单的action属性的页面上,您无需执行其他操作...

If you want to submit it manually by invoking post in javascript you need to remember to prevent the form to be submitted again as that's the input type="submit" element task, you can invoke your post and serializing the entire form data, instead one by one as in your example... 如果要通过调用javascript中的post手动提交,则需要记住防止再次提交表单,因为这是input type="submit"元素任务,您可以调用您的帖子并序列化整个表单数据,而不是一个像您的例子一样...

like: 喜欢:

// when the form is submited...
$("form").submit(function() {

   // submit it using `post`
   $.post('/sportdub/points_dub/', $("form").serialize()); 

   // prevent the form to actually follow it's own action
   return false; 

});

in your dynamic code, you will have those variables as: 在动态代码中,您将拥有以下变量:

supportingTeam = request.POST['supporting_team']
supportingType = request.POST['supporting_type']

and this, will be valid, no matter if you have the manually form submit script or not... 这将是有效的,无论您是否具有手动表单提交脚本...

After your most recent comment, I think you should try using the input radio buttons provided in HTML. 在您发表最新评论之后,我认为您应该尝试使用HTML中提供的输入单选按钮。 It is very easy to do button groupings, make labels for the inputs, and retrieve which one has been clicked. 进行按钮分组,为输入创建标签以及检索已单击的按钮非常容易。

Altering your HTML slightly to look like this (the for attribute of the label allows the user to be able to click the label and select the radio button instead of clicking on the button directly): 稍微更改HTML使其看起来像这样(标签的for属性使用户能够单击标签并选择单选按钮,而不必直接单击按钮):

  <div>
    <input id="team1" type="radio" name="supporting" value="Team1" /><label for="team1">Team 1</label>
    <input id="team2" type="radio" name="supporting" value="Team2" /><label for="team2">Team 2</label>
  </div>
  <div>
    <input id="relaxed" type="radio" name="style" value="relaxed" /><label for="relaxed">Relaxed</label>
    <input id="strict" type="radio" name="style" value="strict" /><label for="strict">Strict</label>
  </div>

I can get which selections have been made using this: 我可以使用以下方法进行选择:

$('input[name="supporting"]').filter(':checked').val();
$('input[name="style"]').filter(':checked').val();

If you still wish to use buttons, class active is added to a radio button when clicked. 如果您仍然希望使用按钮,则单击active类别会添加到单选按钮。 To get the button text, try: 要获取按钮文本,请尝试:

$('button[name="supporing"]').filter('.active').text();
$('button[name="style"]').filter('.active').text();

To put this value into a hidden input, do the following: 要将此值放入隐藏的输入中,请执行以下操作:

$('#hiddenInput1').val($('button[name="supporting"]').filter('.active').text());
$('#hiddenInput2').val($('button[name="style"]').filter('.active').text());

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

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