2012-11-12 102 views
2

在我的Django應用程序中,我想使用Twitter引導程序單選按鈕。然後我想發佈這些按鈕的值以創建一個對象。帶單選按鈕的jQuery post功能

這裏是我的按鈕:

<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"> 

我想擺脫這些單選按鈕的信息,並在視圖中創建一個對象匹配:

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) 

的事情是,我不太瞭解JavaScript,所以我沒有找到如何從按鈕獲取值。

然後,我想我必須使用:

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

可是我該怎麼做,這樣的風格和配套對應按鈕的值,然後將它張貼僅當在用戶點擊按鈕?

非常感謝您的幫助。

+0

所以......你爲什麼不使用我的代碼從其他答案?什麼問題?所有你需要做的就是'alignment = request.POST ['alignment']' – balexandre

回答

2

你最近的評論後,我想你應該嘗試使用HTML提供的輸入單選按鈕。做按鈕分組非常簡單,爲輸入製作標籤並檢索哪個被點擊。

改變你的HTML稍微看起來像這樣(在該標籤的屬性,讓用戶能夠點擊標籤,選擇單選按鈕,而不是點擊按鈕直接):

<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> 

我能得到哪些選項一直在用這個做:

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

如果您仍然希望使用的按鈕,點擊後級active被添加到一個單選按鈕。爲了得到按鈕上的文字,請嘗試:

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

爲了把這個值放入一個隱藏的輸入,請執行以下操作:

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

好的,但我有兩個不同的值。我怎樣才能得到它們,然後只有在用戶點擊提交按鈕時纔將它們傳遞給我的發佈功能? –

+0

@JulietteDupuis我已經更新了我的答案,向您展示瞭如何獲取僅帶有名稱風格的按鈕的文本。你只是試圖獲得具有風格名稱的按鈕的文本?或者您是否在嘗試查找用戶點擊了哪些按鈕? – Kyle

+0

感謝您的幫助。我試圖找到用戶點擊「風格」和「支持」的按鈕。然後將這些信息傳遞給post函數,以便在我的視圖測試中使用它。 –

3

考慮我last answer,讓我們用自己的代碼...在你的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> 

,你只需要添加一個隱藏字段每塊,你的情況,添加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> 

添加一個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()); 
}); 

當您單擊create your match整個窗體將被張貼到設定的該action屬性頁形式,你不得不做更多...

如果你想通過調用JavaScript中,你需要記住,以防止再次提交表單作爲這就是input type="submit"元素任務post手動提交它,你可以調用您的文章,並通過序列化整個表格的數據,而不是一一個在你的榜樣......

喜歡:

// 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; 

}); 
在動態代碼

,你將有這些變量爲:

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

這和,這將是有效的,無論你是否有手動表單提交腳本或不...

+0

非常感謝。我試圖做到這一點,然後我認爲使用按鈕的「數據值」來創建我的對象與發佈的數據會更容易。所以我嘗試使用$(「#id_of_radiogroup .active」)。data(「value」);現在,我想在隱藏的輸入中傳遞這個值,而不使用(click。()),因爲我不想檢查默認的按鈕,我不想讓空的選擇。所以我試圖找出如何將按鈕的數據值傳遞給隱藏的輸入。無論如何,非常感謝你的答案,你真的幫了我很多。 –